Further Reading extended read events and callbacks in Winston Winston event and callback
Each instance of Winston. Logger is also a instance of an eventemitter. A log event would be raised each time a transport successfully logs a message:
Logger. On (' Loggingfunction (logger. info (' CHILL winston!', {seriouslytrue});
Working with multiple loggers in Winston using multiple Winston in logger
Often in larger, more complex applications it's necessary to has multiple logger instances with different settings. Each logger are responsible for a different feature area (or category). This is exposed in winston
Ways:through and winston.loggers
instances of winston.Container
. In fact, is winston.loggers
just a predefined instance of winston.Container
:
Define multiple log files
1. Use the Add method to add a file
var Winston=Require' Winston‘);//Configure the logger for ' category1 '//Winston.Loggers.Add' Category1', {console: {level:' Sillytrue, Label: "}, File: {Filename '/path/to/some/file '}}); ////Configure the Logger for ' Category2 ' //winston. loggers. add ( ' Category2 ", {Couchdb ' 127.0.0.1 ', Port5984}});
Filters and Rewriters filtering logs and overrides
Filters allow modifying the contents of logs messages, and rewriters allow modifying the contents of log meta e.g. to mask Data that should not appear in logs.
Filters allows you to modify the contents of a log message, and Rewriter allows you to modify the contents of the log metadata. To mask data that should not appear in the log.
Both filters and Rewriters is simple Arrays of functions which can is provided when creating a new winston.Logger(options)
. E.g.:
Winston. Logger ({ rewriters: [function (/* etc etc *}], filters: [function ( //How to define????? })
Like any Array they can also is modified at runtime with no adverse side-effects to the winston
internals.
Logger. filters. Push (function (meta.: Msg;}); logger. info (' transaction with card number 123456789012345 successful.');
This could result in the this output:
info: transaction with card number 123456****2345 successful.
This method is aged after information processing, the returned value is then stored in the log file
Adding Custom Transports Add customization transports
Adding a custom transport is actually pretty easy. All need to does is accept a couple of options, set a name, implement a log () method, and add it to the set of transport S exposed by Winston.
Adding custom transports is actually easy. All you need to do is accept several options, set name, implement the log () method, and add it to the Winston exposed transports.
var util=Require' Util'), Winston=Require' Winston‘);var Customlogger=Winston.Transports.Customlogger=function (Options) {//Name this Logger//This.Name=' Customlogger‘;//Set the level from your options//This.Level=Options.Level||' Info‘;//Configure your storage backing as you see fit// };////Inherit from ' Winston. Transport ' So's can take advantage //of the base functionality and '. Handleexceptions () '. //util.inherits (Customlogger, winston.< Span class= "Pl-smi" >transport); customlogger. prototype. log = function (level, msg, meta, callback) {/// /Store This message and metadata, maybe use some custom logic //then callback indicating success.
//
callback (null, true);};
Custom log format log to specify custom log formats you should set formatter function for transport. Currently supported transports Are:console, File, Memory. The Options object is passed to the Format function. It ' s general properties Are:timestamp, level, message, Meta. Depending on the transport type is additional properties.to specify a custom log format, you should set the Format function for the transport. The currently supported transports are: Console,file,memory. the Options object is passed to the Format function. Its general properties are: Timestamp,level,message,meta. There may be other properties depending on the transport type.
var logger=New (Winston.Logger) ({transports: [New (Winston.Transports.Console) ({Timestamp:function () {ReturnDate.Now (); },Formatter:functionOptions) {Return string is passed to logger.ReturnOptions.Timestamp ()+‘‘+Options.Level.toUpperCase ()+‘‘+ (Undefined!==Options.Message?Options.Message: ' "+ (options . meta && object. Keys (options. META). length ' \n\t ' + Span class= "PL-C1" >json. stringify (options. Meta) ' logger. info ( ' Data to Log.
Winston Log Management 3