Package Exports
- better-logging
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (better-logging) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
better-logging
Javascript comes by default with different standardized logging types. But as it stands only browsers are taking advantage of these different methods of logging. better-logging
aims to improve the default logging experience of any node application, it is designed to be a drop in replacement for the default logging methods.
Since better-logging
only decorates the default logging methods you won't lose any functionality provided by other tooling. better-logging
is not meant to be the be all and end all of node.js logging. It is just supposed to increase the usefulness of the default logging methods.
Install: npm i better-logging
Upgrading from major version 3 to major version 4? See the upgrade guide.
// Default in node.js
console.debug('foo'); // foo
console.log('foo'); // foo
console.info('foo'); // foo
console.warn('foo'); // foo
console.error('foo'); // foo
// With better-logging
require('better-logging')(console);
console.debug('foo'); // [11:46:35] [debug] foo
console.log('foo'); // [11:46:35] [log] foo
console.info('foo'); // [11:46:35] [info] foo
console.warn('foo'); // [11:46:35] [warning] foo
console.error('foo'); // [11:46:35] [error] foo
console.line('foo'); // foo
console.logLevel /**
* debug: 4
* log: 3
* info: 2
* warn: 1
* error: 0
* line: 1
* turn off all logging: -1
* default: 3
*/
Better-logging calls the default implementation in the background.
require('better-logging')(console);
console.info('Hello World');
// Is the same as
console.info('[11:46:35] [info] Hello World')
It can sometimes be useful to define your own logging style, for those occasions you can overwrite the default formatting function:
const chalk = require('chalk');
require('better-logging')(console, {
format: ctx => `${ctx.time24} ${ctx.time12} ${ctx.date} ${ctx.type} ${ctx.unix} ${ctx.STAMP('lel', chalk.blue)} ${ctx.msg}`
});
console.debug('foo'); // [11:44:40] [11:44:40 AM] [2/2/2019] [debug] [1549104280572] [lel] foo
console.log('foo'); // [11:44:40] [11:44:40 AM] [2/2/2019] [log] [1549104280574] [lel] foo
console.info('foo'); // [11:44:40] [11:44:40 AM] [2/2/2019] [info] [1549104280577] [lel] foo
console.warn('foo'); // [11:44:40] [11:44:40 AM] [2/2/2019] [warn] [1549104280579] [lel] foo
console.error('foo'); // [11:44:40] [11:44:40 AM] [2/2/2019] [error] [1549104280580] [lel] foo
Some times the default log levels might not fit your needs, in those cases you can redefine the log levels to anything you like.
require('better-logging')(console, {
logLevels: {
debug: 0,
error: 10,
info: 10,
line: 10,
log: 10,
warn: 10,
}
});
console.debug('foo'); // [11:46:35] [debug] foo
console.log('foo'); // wont print
console.info('foo'); // wont print
console.warn('foo'); // wont print
console.error('foo'); // wont print
console.line('foo'); // wont print
It's finally time for the most important option of them all... colors!
const chalk = require('chalk');
require('better-logging')(console, {
color: {
base: chalk.greenBright,
type: {
debug: chalk.magentaBright,
info: chalk.magentaBright,
log: chalk.magentaBright,
error: chalk.blue,
warn: chalk.blue,
}
},
});
// The type color decides the color of the word inside the "ctx.type" stamp.
// By default the text "info" in this stamp, [info], is white, but now it can be any color you want (or that your terminal supports) :)
Sometimes you might not want all log arguments to be formatted and converted into a string. For these cases you can change the message construction strategy by passing a MessageConstructionStrategy
enum in the configuration object.
const { MessageConstructionStrategy } = betterLogging;
betterLogging(console, {
messageConstructionStrategy: MessageConstructionStrategy.ALL,
});
Strategies:
- ALL (default): Will consume all arguments and format them as a single string.
- FIRST: Will consume just the first argument and format it as a string, it will then spread the rest of the arguments into the implementation call.
- NONE : Won't format any arguments, HOWEVER the format function will be called with an empty string as the message.
Express middleware
I've found that i keep writing the same middleware function (for logging incoming requests) over and over again whenever i start a new project, so i decided to include it in the library from the get-go. (Don't be alarmed, its very small, it won't add that much to your bundle if you don't use express).
const betterLogging = require('better-logging');
const app = require('express')();
// Init betterLogging
betterLogging(console);
// Setup middleware
app.use(betterLogging.expressMiddleware(console));
app.listen(8080);
Default config:
app.use(betterLogging.expressMiddleware(console, {
method: {
show: true,
color: chalk.grey,
order: 1,
},
ip: {
show: true,
color: chalk.grey,
order: 2,
},
path: {
show: true,
color: chalk.reset
order: 3,
},
body: {
show: false,
color: chalk.reset
order: 4,
},
header: {
show: false,
color: chalk.reset
order: 5,
}
}));
Typescript support
// using import
import betterLogging from 'better-logging';
betterLogging(console);
console.log('Hello!') // [11:46:35] [log] Hello!
// using require
require('better-logging').default(console);
console.log('Hello!') // [11:46:35] [log] Hello!
Decorate any object
Support for decoration of arbitrary objects is considered experimental, this is due to problems with typescript support. If you intend to use better-logging purely with javascript or dont care about type support, then everything should work just fine out of the box. However if you intend to use better-logging with typescript then you should be aware that the types for the console object are hardcoded and will show up on the console object even if you chose not to decorate it. This means that console.line('foo')
will look ok to typescript, but will fail during runtime.
const better = {};
require('better-logging')(better);
better.debug('foo'); // [11:46:35] [debug] foo
better.log('foo') // [11:46:35] [log] foo
better.info('foo'); // [11:46:35] [info] foo
better.warn('foo'); // [11:46:35] [warning] foo
better.error('foo'); // [11:46:35] [error] foo
better.line('foo'); // foo
better.logLevel = 0;
// When decorating an arbitrary object we need to trick the type system into thinking that better-logging might infact fail to decorate our object.
const better = {}; // due to some "strange" behavior with the typescript type system this has to be CONST.
if (!require('better-logging').default(better)) throw 'This will never happen';
better.log('Hello!') // [11:46:35] [log] Hello!
Custom Instance
First of all, the custom instance was designed to be used internally to make TDD easier to implement. However some advanced users might find the need to overwrite the default behavior of better-logging on a more detailed level than the current api allows. The custom instance will not log anything to the console, but will instead call the corresponding method on the implementation object. For example, calling better.info('Hi!')
will call implementationObj.info('[11:46:35] [info] Hi!')
. The msg
parameter of these functions will be the fully formatted string that usually ends up logged to the terminal, and the args
array is the arguments that was NOT formatted (determined by the message construction strategy).
const { CustomInstance } = require('better-logging');
const implementationObj = {
log: (msg, ...args) => {},
info: (msg, ...args) => {},
debug: (msg, ...args) => {},
error: (msg, ...args) => {},
warn: (msg, ...args) => {}
};
const customLogging = CustomInstance(implementationObj);
const better = {};
customLogging(better);
For reference, this is how you would recreate the default instance of better-logging.
const { CustomInstance } = require('better-logging');
const betterLogging = CustomInstance(console);
const better = {};
betterLogging(better);
better.log('Works!');
License
See LICENSE