Logger File string interpolation problem

Hi, when i try to write a log for a file, occurs the follow:

info: serving app on http://%s:%s

config/app.js

transport: file,

    file: {
      driver: 'file',
      name: 'adonis-app',
      filename: 'adonis.log',
      level: 'info',
    }

For console everything works.

1 Like

Could you provide the log statement?

2 Likes

Could you add quotes to the file value:

transport: 'file'

and try again?

1 Like

@ndianabasi quotes did already at file value, I forgot to write on the post.

async authenticate({ request, response, auth }) {
    try {
      const { email, password } = request.all();

      const token = await auth.attempt(email, password);

      return token;
    } catch (error) {
      Logger.info('%s', 'Hello World')
      return response.unauthorized({ error: 'Email or password is invalid.' })
    }
  }

How I said if I switch transport for console everything works fine.

Someone else had this problem?

I had the same problem. To resolve it I took a look at the Logger File and Console classes to compare what was different.

It turned out the File class doesn’t have the config winston.format.splat() that is responsible to parse the string.

So, I updated my config/app.js and add the formats to my file driver configuration, like this:

const winston = require('winston')

    file: {
      driver: 'file',
      name: Env.get('APP_NAME'),
      filename: 'logs/adonis.log',
      level: Env.get('LOG_LEVEL', 'info'),
      format: winston.format.combine(
        winston.format.splat(),
        winston.format.json()
      )
    }

I read the Winston docs to find out the correct format: https://www.npmjs.com/package/winston#formats

Hope it can help to sort it out.

2 Likes