Skip to content

Commit 6bfd1ad

Browse files
authored
Merge pull request #2 from trivialsoftware/null-logging
Added null logger
2 parents 830c3e4 + c6c4d59 commit 6bfd1ad

File tree

4 files changed

+118
-3
lines changed

4 files changed

+118
-3
lines changed

Readme.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ const basicLogger = logging.getLogger('basic');
5959
const overriddenLogger = logging.getLogger('overridden', { streams: [ /* ... */ ] });
6060
```
6161

62-
#### Usage with Unit Tests
62+
### Usage with Unit Tests
6363

64-
Generally, when you're running unit tests, you don't want most logging. The easiest way to achieve this is by setting the `LOG_LEVEL` environment variable to `'ERROR'`. You can do this at the top of your unit test:
64+
Generally, when you're running unit tests, you don't want most logging. The easiest way to achieve this is by setting
65+
the `LOG_LEVEL` environment variable to `'ERROR'`. You can do this at the top of your unit test:
6566

6667
```javascript
6768
// Set `LOG_LEVEL` before importing your code that imports `trivial-logging`
@@ -74,6 +75,37 @@ const { expect } = require('chai');
7475

7576
Make sure that you set `process.env.LOG_LEVEL` _before_ importing any code that imports `trivial-logging`.
7677

78+
### Null Logger (Disabling all logging)
79+
80+
Trivial Logging has a `NullLogger` implementation. This turns all logging operations into a noop. This means that there
81+
will be no output, and very little overhead from logging calls. This is helpful with debugging, or for unit tests.
82+
83+
This can be enabled by setting `LOG_NULL` to any value. For example:
84+
85+
```bash
86+
$ env LOG_NULL="true" node ./example.js
87+
```
88+
89+
This can also be enabled via the config file, with the `nullLogger` property being set to true:
90+
91+
```javascript
92+
const logging = require('trivial-logging');
93+
94+
const config = {
95+
nullLogger: true
96+
};
97+
98+
// This stores the configuration
99+
logging.init(config);
100+
101+
// This gets a null logger
102+
const logger = logging.getLogger('some logger');
103+
104+
// All calls to this logger are noops
105+
logger.info("Some logging.");
106+
logger.fatal("Some other logging.");
107+
```
108+
77109
## API
78110

79111
### `init(config)`

example/null-logging-example.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//----------------------------------------------------------------------------------------------------------------------
2+
// logging-example.js - Brief description for logging-example.js module.
3+
//
4+
// @module
5+
//----------------------------------------------------------------------------------------------------------------------
6+
7+
const logging = require('../logging');
8+
9+
//----------------------------------------------------------------------------------------------------------------------
10+
11+
logging.init({ debug: true, nullLogger: true });
12+
logging.setRootLogger('example');
13+
14+
//----------------------------------------------------------------------------------------------------------------------
15+
16+
const logger = logging.loggerFor(module);
17+
18+
logger.trace('more tracing!');
19+
logger.debug('testing?');
20+
logger.info('This is a test.', logger.dump({ msg: 'with an object' }), logger.dump({ other: { nested: { object: { of: { doom: true, foo: 23, bar: 'apples', baz: null } } } }}));
21+
logger.warn('This is a warning. No object this time.');
22+
logger.error('This is an error.', new Error('TESTING!!!!! And stuff.'));
23+
24+
const logger2 = logging.getLogger('other logger');
25+
26+
logger2.trace('more tracing!');
27+
logger2.debug('testing?');
28+
logger2.info('This is a test.', logger.dump({ msg: 'with an object' }));
29+
logger2.warn('This is a warning. No object this time.');
30+
logger2.error('This is an error.', new Error('TESTING!!!!! And stuff.'));
31+
32+
//----------------------------------------------------------------------------------------------------------------------

lib/nullLogger.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//----------------------------------------------------------------------------------------------------------------------
2+
// nullLogger.js - Brief description for nullLogger.js module.
3+
//
4+
// @module
5+
//----------------------------------------------------------------------------------------------------------------------
6+
7+
const { EventEmitter } = require('events');
8+
9+
//----------------------------------------------------------------------------------------------------------------------
10+
11+
class NullLogger extends EventEmitter
12+
{
13+
// Bunyan Properties
14+
get stdSerializers(){ return {}; }
15+
16+
// Bunyan API
17+
child(){ return new NullLogger(); }
18+
level(){ return 'INFO'; }
19+
levels(){ return ['INFO']; }
20+
addSerializers(){}
21+
addStream(){}
22+
reopenFileStreams(){}
23+
close(){}
24+
25+
// Logging API
26+
trace(){}
27+
debug(){}
28+
info(){}
29+
warn(){}
30+
error(){}
31+
fatal(){}
32+
} // end NullLogger
33+
34+
//----------------------------------------------------------------------------------------------------------------------
35+
36+
module.exports = {
37+
createLogger(){ return new NullLogger(); }
38+
}; // end exports
39+
40+
//----------------------------------------------------------------------------------------------------------------------

logging.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ const { inspect } = require('util');
88
const path = require('path');
99

1010
const _ = require('lodash');
11-
const logging = require('bunyan');
1211
const bunyanDebugStream = require('bunyan-debug-stream');
1312
const colors = require('colors');
1413

14+
let logging = require('bunyan');
15+
1516
//----------------------------------------------------------------------------------------------------------------------
1617

1718
class LoggingService {
@@ -27,6 +28,11 @@ class LoggingService {
2728
this.mainDir = process.cwd();
2829
} // end try
2930

31+
if(process.env.LOG_NULL)
32+
{
33+
logging = require('./lib/nullLogger');
34+
} // end if
35+
3036
// Setup default streams
3137
this.streams = [
3238
{
@@ -51,6 +57,11 @@ class LoggingService {
5157
// Pull out the streams config, with sane defaults.
5258
const streams = ((config.logging || config).streams) || this.streams;
5359

60+
if(config.nullLogger)
61+
{
62+
logging = require('./lib/nullLogger');
63+
} // end if
64+
5465
this.streams = _.map(streams, (stream) =>
5566
{
5667
// We only _ever_ override the process.stdout steam.

0 commit comments

Comments
 (0)