winstonjs / winston-daily-rotate-file

A transport for winston which logs to a rotating file each day.
MIT License
889 stars 151 forks source link

cant use query function if logs are not stored in json format #324

Closed ocdkerosine closed 2 years ago

ocdkerosine commented 3 years ago

hey guys, so it seems in order for query() to work it needs a valid json string and i am storing in plain text.

i saw from another issue that json needs to be enabled for this to the query() to work or am i missing something?

also the logger keeps adding [31m and ```[39m] to the file transport

see below

FORMATTER CONFIG

export const jsonFormatter = (logEntry: any) => {
  const base = { timestamp: new Date() }
  const json = _.merge(base, logEntry)
  const today = new Date()
  const day = today.toISOString().split('T')[0]
  const hours = today.getUTCHours()
  const minutes = today.getUTCMinutes()
  logEntry['message'] = `[${day} ${hours}:${minutes} UTC]: ${json.message}`
  return logEntry
}

winston.createLogger({
    level: 'info',
    format: combine(
      colorize(),
      winston.format(jsonFormatter)(),
      printf((info: any) => `${info.level}: ${info.message}`)
    ),
    transports: [
      new winston.transports.DailyRotateFile({ 
      json: true, 
      dirname: path.join(__dirname, '../../../logs/error'), 
      filename: 'error-%DATE%.log', 
      datePattern: 'YYYY-MM-DD-HH',
      createSymlink: true, 
      symlinkName: 'error.log', 
      maxFiles: '30d', level: 'error', 
      maxSize: '20m', 
      utc: true, 
      zippedArchive: true }),
})

SAMPLE LOG

FILE

 [31merror [39m: [2021-07-21 1:8 UTC]: Recieved kill command. Gracefully shutting down.
 [31merror [39m: [2021-07-21 1:9 UTC]: Recieved kill command. Gracefully shutting down.
 [31merror [39m: [2021-07-21 1:10 UTC]: Recieved kill command. Gracefully shutting down.
 [31merror [39m: [2021-07-21 1:11 UTC]: Recieved kill command. Gracefully shutting down.
 [31merror [39m: [2021-07-21 1:13 UTC]: Recieved kill command. Gracefully shutting down.
 [31merror [39m: [2021-07-21 1:19 UTC]: Recieved kill command. Gracefully shutting down.
 [31merror [39m: [2021-07-21 1:21 UTC]: Recieved kill command. Gracefully shutting down.
 [31merror [39m: [2021-07-21 1:25 UTC]: Recieved kill command. Gracefully shutting down.

CONSOLE

 info: [2021-07-21 2:3 UTC]: Shutting down.
 info: [2021-07-21 2:3 UTC]: Server shutdown successful. Closing DB connection.
 info: [2021-07-21 2:3 UTC]: Shutdown complete.

RESULT OF FILE TRANSPORT QUERY

[1] {
[1]   dailyRotateFile: SyntaxError: Unexpected token n JSON at position 0
[1]       at JSON.parse (<anonymous>)
[1]       at add (/test/node_modules/winston-daily-rotate-file/daily-rotate-file.js:303:32)
[1]       at ReadStream.<anonymous> (/test/node_modules/winston-daily-rotate-file/daily-rotate-file.js:257:17)
[1]       at ReadStream.emit (events.js:376:20)
[1]       at ReadStream.emit (domain.js:470:12)
[1]       at addChunk (internal/streams/readable.js:309:12)
[1]       at readableAddChunk (internal/streams/readable.js:280:11)
[1]       at ReadStream.Readable.push (internal/streams/readable.js:223:10)
[1]       at internal/fs/streams.js:186:14
[1]       at FSReqCallback.wrapper [as oncomplete] (fs.js:561:5)
[1] }

anyone got any ideas, thanks