Pino: Set log directory

Created on 17 Jun 2017  路  7Comments  路  Source: pinojs/pino

can anyone tell me how to set custom log directory and log file name in pino?
Suppose I want my application to generate log files in /logger/mylogger.log file. How to do so?

thanks in advance

question

All 7 comments

You need to pass in a stream: pino(opts, fs.createWriteStream(dest))

@mcollina's answer is if you want to do write directly to the file from within your application. We also have documentation that promotes easy log rotation -- https://github.com/pinojs/pino/blob/master/docs/howtos.md#rotate

The way we recommend is to write to stdout and redirect

node app.js > /logger/mylogger.log

But as @mcollina says, if you can't/won't do that for any reason:

const fs = require('fs')
const opts = {}
pino(opts, fs.createWriteStream('/logger/mylogger.log'))

I feel like we've answered this, closing - feel free to open

On a side note, @mcollina @jsumners - I think somewhere in the doc reorg etc. we've lost some basic fundamentals (or maybe they were never written down) - maybe we should write a docs/philosophy.md - sort of a compliment to how tos.

@davidmarkclements yes, it's just really difficult to get everything in a concise Readme that won't put people off.

yah for sure - we just need to distil some stuff a bit more I think - the recommendation to redirect output or pipe to a separate process is covered quite a lot in issues - so maybe we should make that (and other reoccurring subjects) prominent

Thank you all.
This piece of code worked for me.

const fs = require('fs')
const opts = {}
pino(opts, fs.createWriteStream('/logger/mylogger.log'))

I have one more question. How can I customize my log content?

In mylogger.log file, it is logging :

{"pid":11028,"hostname":"MM005820","level":30,"time":1497841117668,"msg":"Server started at port 4000","v":1}

I need this format:
{"level":"error","message":"Error","timestamp":"2017-06-19T07:07:10.677Z"}

You should be using a transport if you want to alter the stream -- https://github.com/pinojs/pino/blob/master/docs/transports.md

Pino's log lines are generic so that they can be processed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

acro5piano picture acro5piano  路  3Comments

ptommasi picture ptommasi  路  3Comments

stephenmathieson picture stephenmathieson  路  6Comments

P4sca1 picture P4sca1  路  4Comments

NicolaiSchmid picture NicolaiSchmid  路  4Comments