Skip to content

Commit

Permalink
Merge pull request #12 from Goodluckhf/dev
Browse files Browse the repository at this point in the history
Format mongo ObjectId
  • Loading branch information
Goodluckhf committed Aug 23, 2018
2 parents c1b90a4 + ac7f78d commit e74646b
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 1 deletion.
Binary file modified docs/example.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/example_terminal.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions example.js
@@ -1,7 +1,9 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import bunyan from 'bunyan';
import BSON from 'bson';
import StdoutStream from './src/';


const logger = bunyan.createLogger({
name : 'exampleLogger',
streams: [
Expand All @@ -17,6 +19,8 @@ const logger = bunyan.createLogger({
(function test() {
const error = new Error('Error here!');
error.extraParams = {
_id: BSON.ObjectId('5b6f28ac588b35524fd5edee'),

testNestedObject: {
value: 10,
},
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -34,6 +34,7 @@
"author": "Goodluckhf <Goodluckhf@yandex.ru>",
"license": "MIT",
"dependencies": {
"bson": "3.0.2",
"cli-color": "1.2.0",
"deep-extend": "0.6.0",
"moment": "2.22.2"
Expand Down
8 changes: 8 additions & 0 deletions src/index.js
@@ -1,4 +1,5 @@
import deepExtend from 'deep-extend';
import BSON from 'bson';
import defaultConfig from './config';
import OptionLineFormatter from './formatters/OptionLine';
import ArrayFormatter from './formatters/Array';
Expand Down Expand Up @@ -66,6 +67,13 @@ export default class BunyanStdoutStream {
return this.config.colors.object.system('Function');
}

if (variable instanceof BSON.ObjectId) {
const startSequence = this.config.colors.object.system('ObjectId("');
const endSequence = this.config.colors.object.system('")');

return `${startSequence}${variable.toString()}${endSequence}`;
}

if (typeof variable === 'object') {
return this.objectFormatter.format(variable, depth, this.formatVariable.bind(this));
}
Expand Down
10 changes: 9 additions & 1 deletion test/common.test.js
@@ -1,5 +1,5 @@
import { expect } from 'chai';

import BSON from 'bson';
import BunyanStdoutStream from '../src';

describe('BunyanStdoutStream', function () {
Expand All @@ -22,6 +22,14 @@ describe('BunyanStdoutStream', function () {
expect(result).to.be.a('string');
});


it('Should parse objectId string', () => {
const objectIdString = '5b6f28ac588b35524fd5edee';
const testValue = { id: BSON.ObjectId(objectIdString) };
const result = this.bunyanStream.formatVariable(testValue);
expect(result).to.be.include(objectIdString);
});

describe('method "formatVariable"', () => {
const testValues = [
'test',
Expand Down

0 comments on commit e74646b

Please sign in to comment.