This library is a wrapper of winston logging library that adds additional beheaviour dedicated for our use.
var logger = require('cf-logs');
logger.info("info message");
logger.error("error message");
Notice that if you use this option then you will not be able to use the DEBUG environment variable to filter these logs.
So our main use should be with the advanced usage.
You can provide a namespace for your current logger, just like in 'debug' module and then controll the output logs with DEBUG environment variable
var logger = require('cf-logs').Logger("codefresh:example");
logger.info("info message");
logger.debug("debug meesage"):
This is very powerfull for development, because many times you want to see only logs from a specific part of the application so you can for example set the DEBUG environment variable to for example: "codefresh:builds".
The default is set so that all logs that begins with a namespace 'codefresh,codefresh:*' will be shown.
var logger = require('cf-logs').Logger("codefresh:example");
var error = new Error("very bad error");
logger.error("error: %s", error.toString());
logger.error("stack: %s", error.stack);
The options that can be passed are:
var options = {
filePath: String,
console: Boolean,
loggly:{
subdomain: String,
inputToken: String
},
showNamespace: Boolean,
env_module: String,
showRequestId: Boolean,
level: String(one of: "error/warn/info/debug"),
consoleOptions: {
formatter: function(options) {
// Receives the consoleOptions object, merged with the meta objects
// Should return a formatted string for this log.
// This is an example:
return new Date().toISOString() +' '+ options.level.toUpperCase() +' >> '+ (undefined !== options.message ? options.message : '') +
(options.meta && Object.keys(options.meta).length ? ' << ' + JSON.stringify(options.meta) : '' );
}
},
basePath: String,
baseNamepsace: String
}
var logger = require('cf-logs');
logger.setGlobalOptions(options);
Set 'filePath' field to an absolute path to a file save the logs to a file. The default is set to null which means no logging to file will happen.
Set 'console' filed to true or false to log to the console. The default is set to true
The logging level is an additional filter that can be set, mainly for production usage.
we have 4 levels, from highest to lowest priority:
- error
- warn
- info
- debug
If not set, the default logging level is 'debug'.
For example: if we set the logging level to warn, then only error and warn logs will be handled. info and debug will not be handled.
Set 'showNamespace' field to true or false to show the namespace in the logs The default is false
You can set 'env_module' field to a name that will be shown on all logs. for example: "cf-api-machine-1" The default is set to null which will not print anything
Set 'showRequestId' field to true or false to show the current request id if exists The default is set to false
Set the basePath to your project root (__dirname), then create loggers with __filename as the namespace. Also, set your base namespace. (e.g. lalala) Result: basePath: /home/user/workspace/projectDir namespace: projectName newLogger(//home/user/workspace/projectDir/internalDir/file.js) will get the namespace: projectName:internalDir:file