Skip to content

Commit

Permalink
Add withHostname option (#25)
Browse files Browse the repository at this point in the history
Co-authored-by: damiankociuba <[email protected]>
  • Loading branch information
damian-kociuba and damiankociuba authored Jun 30, 2021
1 parent e7d9e05 commit 4de3c5f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ accessors, though, and invalid values will be ignored.
or add the same as a property (if objects). Default: `false`.
- **withLevel**: Will prepend (string) or add property (object) indicating the
log level. Default: `true`.
- **withHostname**: Will prepend(string) or add property (object) indicating the
hostname from which the log was sent. Default: `false`.
- **withStack**: If an object is or contains an `Error` object, setting this to
`true` will cause the stack trace to be included. Default: `false.`

Expand Down
21 changes: 21 additions & 0 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const net = require('net');
const os = require('os');
const reconnectCore = require('reconnect-core');
const tls = require('tls');
const urlUtil = require('url');
Expand Down Expand Up @@ -114,6 +115,7 @@ class Logger extends Writable {
this.flattenArrays = 'flattenArrays' in opts ? opts.flattenArrays : opts.flatten;
this.console = opts.console;
this.withLevel = 'withLevel' in opts ? opts.withLevel : true;
this.withHostname = opts.withHostname;
this.withStack = opts.withStack;
this.timestamp = opts.timestamp || false;

Expand Down Expand Up @@ -293,6 +295,7 @@ class Logger extends Writable {
if (_.isObject(modifiedLog)) {
let safeTime;
let safeLevel;
let safeHostname;

if (this.timestamp) {
safeTime = getSafeProp(modifiedLog, 'time');
Expand All @@ -305,6 +308,12 @@ class Logger extends Writable {
modifiedLog[safeLevel] = lvlName;
}

// Set level if present
if (this.withHostname) {
safeHostname = getSafeProp(modifiedLog, 'hostname');
modifiedLog[safeHostname] = os.hostname();
}

modifiedLog = this._serialize(modifiedLog);

if (!modifiedLog) {
Expand Down Expand Up @@ -336,6 +345,10 @@ class Logger extends Writable {
modifiedLog.unshift(lvlName);
}

if (this.withHostname) {
modifiedLog.unshift(os.hostname());
}

if (this.timestamp) {
modifiedLog.unshift((new Date()).toISOString());
}
Expand Down Expand Up @@ -667,6 +680,14 @@ class Logger extends Writable {
this._withLevel = !!val;
}

get withHostname() {
return this._withHostname;
}

set withHostname(val) {
this._withHostname = !!val;
}

get withStack() {
return this._withStack;
}
Expand Down
54 changes: 53 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,21 @@ tape('Logger allows specification of minLevel at construction', function (t) {

});

tape('Logger allows specification of withHostname at construction', function (t) {

const logger1 = new Logger({ token, withHostname: true, region: 'eu' });

t.equal(logger1.withHostname, true, 'withHostname');

const logger2 = new Logger({ token, region: 'eu' });

t.equal(logger2.withHostname, false, 'withHostname');

t.end();

});


// CUSTOM JSON SERIALIZATION

tape('Error objects are serialized nicely.', function (t) {
Expand Down Expand Up @@ -495,6 +510,43 @@ tape('Non-JSON logs may carry timestamp.', function (t) {
logger[lvl]('test');
});

tape('Non-JSON logs may carry Hostname.', function (t) {
t.plan(1);
t.timeoutAfter(2000);

mockTest(function (data) {
t.true(pattern.test(data), 'matched');

});
const os = require('os');
const lvl = defaults.levels[3];
const tkn = token;
const pattern = new RegExp(`^${token} ${os.hostname()} \\w+ test\\n$`);

const logger = new Logger({ token: tkn, withHostname: true, region: 'eu' });

logger[lvl]('test');
});


tape('JSON logs may carry Hostname.', function (t) {
t.plan(1);
t.timeoutAfter(2000);

mockTest(function (data) {
const log = JSON.parse(data.substr(37));
t.equals(os.hostname(), log.hostname);
});
const os = require('os');
const lvl = defaults.levels[3];
const tkn = token;

const logger = new Logger({ token: tkn, withHostname: true, region: 'eu' });

logger[lvl]({msg: "Testing!"});
});


tape('JSON logs match expected pattern.', function (t) {
t.timeoutAfter(2000);

Expand Down Expand Up @@ -873,7 +925,7 @@ tape('Bunyan integration is provided.', function (t) {

t.equal(streamDef.level, defaults.bunyanLevels[3],
'minLevel translated correctly');

t.equal(streamDef.stream._logger._host, 'eu.data.logs.insight.rapid7.com')

const logger = bunyan.createLogger({
Expand Down

0 comments on commit 4de3c5f

Please sign in to comment.