A derivative of winston and morgan to facilitate uniform logging for environments like k8s where all logs go to stdout.
- All the features of winston (levels, standard formatting, etc)
- Preset to all go to stdout
- Standard format, ready for machine consumption
- Args expansion, just like console.log
- Supports strings and all native objects
- Adds a label for the specific type of logging so you can discriminate when analyzing your logs
- Incorporates morgan logging for http logs
- Regex based masking
npm install --save kubernetes-logger
If you used winston before, you can just add the module to the instantiation. Winston was usually used by creating a custom logger function to customize the transports and output, so in your code you would usually just have something like this:
const logger = require('./utils/logger');
logger.info('This is an info message');
logger.error('This is an error');
But now, you would instantiate with a module name, or label:
const logger = require('kubernetes-logger')('app');
logger.info('This is an info message');
logger.error('This is an error');
Output:
2020-05-08T12:22:43.821Z [app] info: This is an info message
2020-05-08T12:22:43.822Z [app] error: This is an error
kubernetes-logger
supports all the log levels supported by winston. For more information checkout the very complete winston documentation.
The default loglevel is info
, but it can be overriden from code:
const logger = require('kubernetes-logger')('web',{loglevel:'http'});
or from an environment variable:
export LOG_LEVEL=debug
!!! note Changing the loglevel from code takes precedence, so if you change the LOG_LEVEL environment variable, it will change it for all kubernetes-logger instances, but you can override from code.
logger.info('com.apple.myapp','This is cool');
logger.info('Test',1,2,3);
logger.info('Total: %d pounds of %s',1000,'chicken');
2020-05-08T12:22:43.821Z [app] info: com.apple.myapp This is cool
2020-05-08T12:22:43.822Z [app] info: Test 1 2 3
2020-05-08T12:22:43.823Z [app] info: Total: 1000 pounds of chicken
logger.info('This is a test',{a:4,b:'6'},1,'another string');
// 2020-05-08T12:22:43.821Z [app] info: This is a test {"a":4,"b":"6"} 1 another string
const logger = require('kubernetes-logger')('app');
const apiLogger = require('kubernetes-logger')('api');
logger.info('This is an info message');
apiLogger.info('This is an API message');
logger.error('This is an error');
// 2020-05-08T12:22:43.821Z [app] info: This is an info message
// 2020-05-08T12:22:43.822Z [api] info: This is an API message
// 2020-05-08T12:22:43.823Z [app] error: This is an error
const morganLogger = require('kubernetes-logger')('web').morgan('combined');
// app could be express or connect
app.use(morganLogger);
// 2020-05-08T11:49:47.480Z [web] http: ::1 - - [08/May/2020:11:49:47 +0000] "GET /images/dh_horz_white.png HTTP/1.1" 200 - "http://localhost:8080/?MRN=asd&ExternalId=SP19-030353" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36"
If you don't use express or connect, you can still use the morgan logger by passing a request, response and fake next function:
const morganLogger = require('kubernetes-logger')('web').morgan('tiny');
// and when you are processing the request
morganLogger(req,res,()=>true);
kubernetes-logger
supports all the out-of-the-box logging formats of morgan, but highly recommends combined
, because it includes the client agent which might be useful in processing your logs and filtering out live and ready probes from real requests. For more information on logging formats, please visit morgan.
const logger = require('kubernetes-logger')('masked',{
masks:[
'MRN=([A-Z|\\d]*)',
'ID=(\\d*)'
]
});
logger.info('/test/?MRN=DY2343&something=FD32342&ID=23432')
2020-05-11T17:41:53.005Z [masked] info: loglevel(LOG_LEVEL): masked info
2020-05-11T17:41:53.006Z [masked] info: /test/?MRN=******&something=FD32342&ID=*****
Masks can also be added later
const logger = require('kubernetes-logger')('maskedLater');
logger.info('123abcXYZ');
logger.addMask('(abc)');
logger.info('123abcXYZ');
2021-07-23T17:27:56.827Z [firstmask] info: loglevel(LOG_LEVEL): maskedLater info
2021-07-23T17:27:56.827Z [firstmask] info: 123abcXYZ
2021-07-23T17:27:56.828Z [firstmask] info: 123***XYZ