Skip to content

Commit

Permalink
Moved pre- and postprocess functions to base, updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
btoll committed May 5, 2017
1 parent 3a19751 commit 9ea2937
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

4 changes: 3 additions & 1 deletion src/format/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module.exports = {
formattedName :
color.color(logMethodName, formattedName);
},
postlog: () => ''
postlog: () => '',
preprocess: () => '',
postprocess: () => ''
};

17 changes: 10 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
// TODO: Throw error when unrecognized log level is given to #setLogLevel.
'use strict';

Expand All @@ -23,10 +24,6 @@ const logLevels = {
ALL: 255
};

// TODO
const preprocess = () => '';
const postprocess = () => '';

const defaultAliases = {
debug: 'info',
fatal: 'error',
Expand Down Expand Up @@ -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);
Expand All @@ -158,7 +155,7 @@ const wrap = methodName =>
);
}

postprocess(methodName);
format.postprocess(methodName);
};

const proto = {
Expand All @@ -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);

0 comments on commit 9ea2937

Please sign in to comment.