Skip to content

Instantly share code, notes, and snippets.

@muratcorlu
Created February 9, 2020 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muratcorlu/4ed1909f8495f5ef6d15c586ce70903a to your computer and use it in GitHub Desktop.
Save muratcorlu/4ed1909f8495f5ef6d15c586ce70903a to your computer and use it in GitHub Desktop.
Node Simple Logger

Simple logging utility for NodeJS v8+.

const { warn } = require('./logger');

warn('Something');

You can set log level via LOG_LEVEL env variable.

# LOG_LEVEL=0 node myfile.js

Values for LOG_LEVEL:

LOG_LEVEL_DEBUG = 0;
LOG_LEVEL_INFO = 1;
LOG_LEVEL_WARN = 2;
LOG_LEVEL_ERROR = 3;
LOG_LEVEL_DISABLED = 4;
const LOG_LEVEL_DEBUG = 0;
const LOG_LEVEL_INFO = 1;
const LOG_LEVEL_WARN = 2;
const LOG_LEVEL_ERROR = 3;
const LOG_LEVEL_DISABLED = 4;
const LOG_LEVEL = process.env.LOG_LEVEL || LOG_LEVEL_ERROR;
const debug = (...args) => LOG_LEVEL <= LOG_LEVEL_DEBUG && console.debug(...args);
const info = (...args) => LOG_LEVEL <= LOG_LEVEL_INFO && console.info(...args);
const warn = (...args) => LOG_LEVEL <= LOG_LEVEL_WARN && console.warn(...args);
const error = (...args) => LOG_LEVEL < LOG_LEVEL_DISABLED && console.error(...args);
const time = (name) => LOG_LEVEL <= LOG_LEVEL_DEBUG && console.time(name);
const timeEnd = (name) => LOG_LEVEL <= LOG_LEVEL_DEBUG && console.timeEnd(name);
module.exports = {
debug,
info,
warn,
error,
time,
timeEnd,
LOG_LEVEL_DEBUG,
LOG_LEVEL_INFO,
LOG_LEVEL_WARN,
LOG_LEVEL_ERROR,
LOG_LEVEL_DISABLED
};
const {
LOG_LEVEL_DEBUG,
LOG_LEVEL_INFO,
LOG_LEVEL_WARN,
LOG_LEVEL_ERROR
} = require('./logger');
describe('Logger', () => {
// const OLD_ENV = process.env;
beforeEach(() => {
jest.resetModules();
// process.env = { ...OLD_ENV };
// delete process.env.LOG_LEVEL;
});
afterEach(() => {
// process.env = OLD_ENV;
delete process.env.LOG_LEVEL;
});
describe('debug()', () => {
const originalLogger = console.debug;
beforeEach(() => {
console.debug = jest.fn();
});
afterEach(() => {
console.debug = originalLogger;
jest.clearAllMocks();
});
it('with LOG_LEVEL_DEBUG', () => {
process.env.LOG_LEVEL = LOG_LEVEL_DEBUG;
const { debug } = require('./logger');
debug('test', 'test2');
expect(console.debug).toBeCalledWith('test', 'test2');
});
it('with LOG_LEVEL_ERROR', () => {
process.env.LOG_LEVEL = LOG_LEVEL_ERROR;
const { debug } = require('./logger');
debug('test', 'test2');
expect(console.debug).not.toBeCalled();
});
});
describe('info()', () => {
const originalLogger = console.info;
beforeEach(() => {
console.info = jest.fn();
});
afterEach(() => {
console.info = originalLogger;
jest.clearAllMocks();
});
it('with LOG_LEVEL_DEBUG', () => {
process.env.LOG_LEVEL = LOG_LEVEL_DEBUG;
const { info } = require('./logger');
info('test', 'test2');
expect(console.info).toBeCalledWith('test', 'test2');
});
it('with LOG_LEVEL info', () => {
process.env.LOG_LEVEL = LOG_LEVEL_INFO;
const { info } = require('./logger');
info('test', 'test2');
expect(console.info).toBeCalledWith('test', 'test2');
});
it('with LOG_LEVEL_ERROR', () => {
process.env.LOG_LEVEL = LOG_LEVEL_ERROR;
const { info } = require('./logger');
info('test', 'test2');
expect(console.info).not.toBeCalled();
});
});
describe('warn()', () => {
const originalLogger = console.warn;
beforeEach(() => {
console.warn = jest.fn();
});
afterEach(() => {
console.warn = originalLogger;
jest.clearAllMocks();
});
it('with LOG_LEVEL_DEBUG', () => {
process.env.LOG_LEVEL = LOG_LEVEL_DEBUG;
const { warn } = require('./logger');
warn('test', 'test2');
expect(console.warn).toBeCalledWith('test', 'test2');
});
it('with LOG_LEVEL_WARN', () => {
process.env.LOG_LEVEL = LOG_LEVEL_WARN;
const { warn } = require('./logger');
warn('test', 'test2');
expect(console.warn).toBeCalledWith('test', 'test2');
});
it('with LOG_LEVEL_ERROR', () => {
process.env.LOG_LEVEL = LOG_LEVEL_ERROR;
const warn = require('./logger').warn;
warn('test', 'test2');
expect(console.warn).not.toBeCalled();
});
});
describe('error()', () => {
const originalLogger = console.error;
beforeEach(() => {
console.error = jest.fn();
});
afterEach(() => {
console.error = originalLogger;
jest.clearAllMocks();
});
it('with LOG_LEVEL_DEBUG', () => {
process.env.LOG_LEVEL = LOG_LEVEL_DEBUG;
const { error } = require('./logger');
error('test', 'test2');
expect(console.error).toBeCalledWith('test', 'test2');
});
it('with LOG_LEVEL_ERROR', () => {
process.env.LOG_LEVEL = LOG_LEVEL_ERROR;
const { error } = require('./logger');
error('test', 'test2');
expect(console.error).toBeCalledWith('test', 'test2');
});
});
describe('time', () => {
const originalLogger = console.time;
beforeEach(() => {
console.time = jest.fn();
});
afterEach(() => {
console.time = originalLogger;
jest.clearAllMocks();
});
it('with LOG_LEVEL_DEBUG', () => {
process.env.LOG_LEVEL = LOG_LEVEL_DEBUG;
const { time } = require('./logger');
time('test');
expect(console.time).toBeCalledWith('test');
});
it('with LOG_LEVEL_ERROR', () => {
process.env.LOG_LEVEL = LOG_LEVEL_ERROR;
const { time } = require('./logger');
time('test');
expect(console.time).not.toBeCalled();
});
});
describe('timeEnd', () => {
const originalLogger = console.timeEnd;
beforeEach(() => {
console.timeEnd = jest.fn();
});
afterEach(() => {
console.timeEnd = originalLogger;
jest.clearAllMocks();
});
it('with LOG_LEVEL_DEBUG', () => {
process.env.LOG_LEVEL = LOG_LEVEL_DEBUG;
const { timeEnd } = require('./logger');
timeEnd('test');
expect(console.timeEnd).toBeCalledWith('test');
});
it('with LOG_LEVEL_ERROR', () => {
process.env.LOG_LEVEL = LOG_LEVEL_ERROR;
const { timeEnd } = require('./logger');
timeEnd('test');
expect(console.timeEnd).not.toBeCalled();
});
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment