diff --git a/README.md b/README.md index fcc27ad..2bdf583 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,22 @@ `onf-logger` wraps any native or non-native logging implementation and provides hooks and aliases for complete customization. -The default behavior is to wrap the `console` object, which is the native logger in most platforms. The can be changed via the `logger.setLogger()` API. +The default behavior is to wrap the `console` object, which is the native logger in most platforms. This can be changed via the `logger.setLogger()` API. + +By wrapping each function, `onf-logger` allows for pre- and postprocessing and pre- and postlogging. + +## Extending the base object + +It is very easy to extend a custom object to handle logging and processing according to one's needs. The object needs to have access to the following functions, either by defining them directly on the object or via delegation: + +- postprocess +- preprocess +- prelog +- postlog + +Have a [look at the source][1], specifically the `wrap` function, to see the order in which they are called, it is very straight-forward. + +It may be simpler to have the object delegate to the provided [`base` object][2] as the [`date` formatter][3] does, and simply override what is needed in the custom formatter object. [![Build Status](https://travis-ci.org/btoll/onf-logger.svg?branch=master)](https://travis-ci.org/btoll/onf-logger) [![Coverage Status](https://coveralls.io/repos/github/btoll/onf-logger/badge.svg?branch=master)](https://coveralls.io/github/btoll/onf-logger?branch=master) @@ -47,3 +62,7 @@ logger.setLogLevel('ALL'); // Same as logger.setLogLevel(255) Benjamin Toll +[1]: /src/index.js +[2]: /src/format/base.js +[3]: /src/format/date.js + diff --git a/src/format/base.js b/src/format/base.js index c069607..3b814bf 100644 --- a/src/format/base.js +++ b/src/format/base.js @@ -11,6 +11,8 @@ module.exports = { formattedName : color.color(logMethodName, formattedName); }, - postlog: () => '' + postlog: () => '', + preprocess: () => '', + postprocess: () => '' }; diff --git a/src/index.js b/src/index.js index 38cc3cd..b68b7f3 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ // TODO: Throw error when unrecognized log level is given to #setLogLevel. 'use strict'; @@ -23,10 +24,6 @@ const logLevels = { ALL: 255 }; -// TODO -const preprocess = () => ''; -const postprocess = () => ''; - const defaultAliases = { debug: 'info', fatal: 'error', @@ -139,7 +136,7 @@ const wrap = methodName => throw new Error('Function does not exist on this logger!'); } - preprocess(methodName); + format.preprocess(methodName); if (methodName === 'raw') { fn.apply(wrapped, arguments); @@ -158,7 +155,7 @@ const wrap = methodName => ); } - postprocess(methodName); + format.postprocess(methodName); }; const proto = { @@ -176,7 +173,13 @@ const proto = { let wrapped = {}; // Defaults to the global console (opt-in for aliases). -setLogger(console, true); +if ( + console && + (typeof console === 'object') && + (typeof console.log === 'function') +) { + setLogger(console, true); +} module.exports = Object.setPrototypeOf(logger, proto);