I want to add timestamp to logs. What is the best way to achieve this?
相关问题
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- google-drive can't get push notifications
- I want to trace logs using a Macro multi parameter
- How to reimport module with ES6 import
- Error message 'No handlers could be found for
相关文章
- node连接远程oracle报错
- how do I log requests and responses for debugging
- How can make folder with Firebase Cloud Functions
- @angular-cli install fails with deprecated request
- node.js modify file data stream?
- How to resolve hostname to an ip address in node j
- Transactionally writing files in Node.js
- Log to node console or debug during webpack build
we could use console-stamp to add timestamp and log level to the existing console:
require('console-stamp')(console, '[yyyy-mm-dd HH:MM:ss.l]')
See https://github.com/starak/node-console-stamp for the details
Although I'm not aware of winston, this is a suggestion. I use log4js for logging & my logs by default look like this
Development is the environment of my node process & [INFO|FATAL] is log level
Maintaining different profiles for logging is possible in log4js. I have Development & Production profiles. Also there are logger types like rolling file appender, console appender, etc. As a addon your log files will be colorful based on the log level [Trace, Info, Debug, Error, Fatal] ;)
log4js will override your console.logIt is a configurable parameter now in 0.5+I was dealing with the same issue myself. There are two ways I was able to do this.
When you include Winston, it usually defaults to adding a Console transport. In order to get timestamps to work in this default case, I needed to either:
The first:
The second, and cleaner option:
Some of the other options for Console transport can be found here:
Above answers did not work for me. In case you are trying to add timestamp to your logs using the latest version of Winston - 3.0.0-rc1, this worked like charm:
I used 'format.combine()'. Since I needed timestamp on all my transports, I added the formatting option within the createLogger, rather than inside each transport. My output on console and on file (activity.log) are as follows:
We can add formatting to this timestamp in 'format.combine()' as usual using:
We can do like this also
You can use built-in util and forever to achieve logging with timestap for your nodejs server. When you start a server add log output as part of the parameter:
And then you can write util in your server.js
server.js
The output will look something like this to out.log file:
out.log