@nr1e/logging
v1.4.1
Published
Provides a standard logging pattern
Downloads
106
Readme
Logging
Provides a simple logging interface for typescript projects that adheres to the NR1E logging standard. The goal of this wrapper is to reduce CPU and memory overhead by only formatting log messages when the log level is set to a level that would allow the message to be logged.
To install using pnpm
pnpm i @nr1e/logging
How to use
Initialize logging in your application.
import * as logging from '@nr1e/logging';
// This only needs to be performed once
logging.initialize({
svc: 'my-service',
level: 'info',
});
To obtain a named child logger of the root logger
const log = logging.getLogger('my-module');
To obtain a named child logger of another logger
const log = logging.getLogger('my-module', parentLogger);
To add a permanent context to a logger
log.ctx({foo: 'bar'});
To log an info message
log.info().msg('Just another day in the life of a logger');
To log a message with a context
log.info().obj({foo: 'bar'}).msg('Just another day in the life of a logger');
You can also log nested objects
log.info().obj({foo: 'bar', nested: {foo: 'bar'}}).msg('Just another day in the life of a logger');
);
To log an error
try {
throw new Error('An error occurred');
} catch (err) {
log.error()
.err(err)
.msg(
'Human sacrifice, dogs and cats living together... MASS HYSTERIA!',
);
}
To log an error with additional context
try {
throw new Error('An error occurred');
} catch (err) {
log.error()
.err(err)
.obj({foo: 'bar'})
.msg(
'Human sacrifice, dogs and cats living together... MASS HYSTERIA!',
);
}