cf-logs

codefresh logs

Usage no npm install needed!

<script type="module">
  import cfLogs from 'https://cdn.skypack.dev/cf-logs';
</script>

README

Coverage Status

Codefresh logging library

This library is a wrapper of winston logging library that adds additional beheaviour dedicated for our use.

Basic Usage - default logger

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.

Advanced Usage - specific namespaced logger

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.

Logging Errors

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);

Configuring the logger

The options that can be passed are:

var options = {
  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);

Logging levels

The logging level is an additional filter that can be set, mainly for production usage. we have 4 levels, from highest to lowest priority:

  1. error
  2. warn
  3. info
  4. 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.

Showing namespace

Set 'showNamespace' field to true or false to show the namespace in the logs The default is false

Global name to the logger

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

Show request id

Set 'showRequestId' field to true or false to show the current request id if exists The default is set to false

Namespace based on file path

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