Skip to content

environment-safe/logger

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@environment-safe/logger

Many years ago, in Java, I developed a preference for bitwise loggers, and hoped someone would eventually release one. That never happened and I adapted to the node highwatermark style of loggers.

Recently though I had a need for detailed, verbose logging that could be minimized, this called not only for more granular control (so you aren't dumping at a rate distorting timing), but also for segmentation of output. For compatibility's sake I included both the syslog and log4j bins. This should cover almost any scenario.

It includes support for bitwise operations on the logger and the log event as well as channels and works in node and the browser.

Usage

The simplest thing to do is use the default static instance

import Logger from '@environment-safe/logger';
//...
Logger.log('foo', Logger.INFO ) //log to info

You can also use a more traditional syntax:

import Logger as logger from '@environment-safe/logger';
const { 
    //if Syslog:
    //eslint-disable-next-line no-unused-vars
    EMERGENCY, ALERT, CRITICAL, ERROR, WARNING, 
    NOTICE, INFORMATIONAL, DEBUG 
    // If log4j: 
    // FATAL, ERROR, WARN, INFO, DEBUG, TRACE
} = logger;

//ADD YOUR OWN:
const MY_LOG_LEVEL = 1 << 9;

//...
logger.log('foo', INFORMATIONAL ) //log to info

You can also configure and use your own instance

import { 
    Logger, makeConsoleChannel 
} from '@environment-safe/logger';
//...
// create a new logger
const logger = new Logger({
    //only log errors and info
    level: Logger.ERROR & Logger.INFO
});
// bind to the built-in console functions
logger.registerChannel(makeConsoleChannel(console));
//log to debug
logger.log('foo', Logger.DEBUG );
// only output if both debug and info are enabled
logger.log('bar', Logger.DEBUG & Logger.INFO );

Testing

npm run test

to run the same test inside the browser:

npm run browser-test

to run the same test inside docker:

npm run container-test

About

A simple logger with fine granular control via bitwise operators, available client and server in native ESM

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • JavaScript 96.5%
  • Dockerfile 3.5%