Skip to content

Commit

Permalink
be able to have custom format for timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
jgallen23 committed Feb 5, 2017
1 parent 5da639e commit 46b07d9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let defaults = {
renderOptions: {
console: {
consoleBell: ['error'],
timestamp: true,
timestamp: 'HH:mm:ss',
pretty: false,
flat: false,
flatColor: false,
Expand Down
3 changes: 2 additions & 1 deletion lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
const stringify = require('json-stringify-safe');
const _ = require('lodash');
const flatten = require('flat');
const timeStamp = require('time-stamp');
const chalk = require('chalk');

module.exports = function(options, tags, message) {
const colors = new chalk.constructor({ enabled: (options.colors) });
const now = new Date();
const ts = (options.timestamp) ? colors.gray(`${now.getHours()}:${now.getMinutes()}:${now.getSeconds()} `) : '';
const ts = (options.timestamp) ? `${colors.gray(timeStamp(options.timestamp))} ` : '';

if (typeof message === 'object') {
if (options.flat === true) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"chalk": "^1.1.3",
"flat": "^2.0.1",
"json-stringify-safe": "^5.0.1",
"lodash": "4.17.2"
"lodash": "4.17.2",
"time-stamp": "^1.0.1"
},
"devDependencies": {
"chai": "^3.5.0",
Expand Down
16 changes: 15 additions & 1 deletion test/logr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('logr', () => {
it('should output to console formatted', () => {
const log = new Logr({ logger });
log(['tag1', 'tag2'], 'message');
expect(lastMessage).to.contain('\u001b[39m\u001b[90m[\u001b[39m\u001b[90mtag1\u001b[39m\u001b[90m,\u001b[39m\u001b[90mtag2\u001b[39m\u001b[90m]\u001b[39m message');
expect(lastMessage).to.contain('\u001b[90m[\u001b[39m\u001b[90mtag1\u001b[39m\u001b[90m,\u001b[39m\u001b[90mtag2\u001b[39m\u001b[90m]\u001b[39m message');
});
it('allows you to specify a tag to trigger a ding', () => {
const log = new Logr({
Expand Down Expand Up @@ -94,6 +94,20 @@ describe('logr', () => {
log(['tag1', 'tag2', 'error'], 'message with a ding added');
expect(lastMessage).to.not.contain('\u0007');
});
it('should allow custom format for timestamp', () => {
const log = new Logr({
logger,
renderOptions: {
console: {
timestamp: 'YYYY',
colors: false
}
}
});
log(['tag1', 'tag2'], 'message');
const year = new Date().getFullYear();
expect(lastMessage).to.equal(`${year} [tag1,tag2] message`);
});
it('should allow disable timestamp', () => {
const log = new Logr({
logger,
Expand Down

0 comments on commit 46b07d9

Please sign in to comment.