Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const options = {
useColors: true, // Enable colors
color: Colors.White, // Set the color of the logger
showTimestamp: true, // Display timestamp in the log message
useLocalTime: false, // Display timestamp in local timezone
showLevel: true, // Display log level in the log message
filename: null, // Set file path to log to a file
appendFile: true, // Append logfile instead of overwriting
Expand Down
2 changes: 1 addition & 1 deletion dist/logplease.min.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion es5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ var defaultOptions = {
useColors: true,
color: Colors.Default,
showTimestamp: true,
useLocalTime: false,
showLevel: true,
filename: GlobalLogfile,
appendFile: true
Expand Down Expand Up @@ -212,7 +213,9 @@ var Logger = function () {

var result = '';

if (this.options.showTimestamp) result += '' + new Date().toISOString() + ' ';
if (this.options.showTimestamp && !this.options.useLocalTime) result += '' + new Date().toISOString() + ' ';

if (this.options.showTimestamp && this.options.useLocalTime) result += '' + new Date().toLocaleString() + ' ';

result = timestampFormat + result;

Expand Down
15 changes: 12 additions & 3 deletions example/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ exports.f = __webpack_require__(1) ? Object.defineProperty : function defineProp
"use strict";


/*
Using the ES5 module for the sake of example.
/*
Using the ES5 module for the sake of example.
To use the regular ES6 version, one would include it with:
const Logger = require('logplease')
*/
Expand All @@ -329,6 +329,7 @@ var Logger = __webpack_require__(15);
var logger1 = Logger.create('daemon', { filename: 'debug.log', useColors: false, appendFile: true });
var logger2 = Logger.create('utils');
var logger3 = Logger.create('logger3', { color: Logger.Colors.Magenta, showTimestamp: false, showLevel: false });
var logger4 = Logger.create('logger4-local-time', { useLocalTime: true });

var red = Logger.create('red', { color: Logger.Colors.Red, showTimestamp: false, showLevel: false });
var green = Logger.create('green', { color: Logger.Colors.Green, showTimestamp: false, showLevel: false });
Expand Down Expand Up @@ -356,6 +357,11 @@ logger3.info('This is an info message #' + number);
logger3.warn('This is a warning message #' + number);
logger3.error('This is an error message #' + number);

logger4.debug('This is a debug message #' + number);
logger4.info('This is an info message #' + number);
logger4.warn('This is a warning message #' + number);
logger4.error('This is an error message #' + number);

red.log('Red log message'); // log() is an alias for debug()
green.log('Green log message');
yellow.log('Yellow log message');
Expand Down Expand Up @@ -448,6 +454,7 @@ var defaultOptions = {
useColors: true,
color: Colors.Default,
showTimestamp: true,
useLocalTime: false,
showLevel: true,
filename: GlobalLogfile,
appendFile: true
Expand Down Expand Up @@ -584,7 +591,9 @@ var Logger = function () {

var result = '';

if (this.options.showTimestamp) result += '' + new Date().toISOString() + ' ';
if (this.options.showTimestamp && !this.options.useLocalTime) result += '' + new Date().toISOString() + ' ';

if (this.options.showTimestamp && this.options.useLocalTime) result += '' + new Date().toLocaleString() + ' ';

result = timestampFormat + result;

Expand Down
10 changes: 8 additions & 2 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

/*
Using the ES5 module for the sake of example.
/*
Using the ES5 module for the sake of example.
To use the regular ES6 version, one would include it with:
const Logger = require('logplease')
*/
Expand All @@ -10,6 +10,7 @@ const Logger = require('../es5/index');
const logger1 = Logger.create('daemon', { filename: 'debug.log', useColors: false, appendFile: true });
const logger2 = Logger.create('utils');
const logger3 = Logger.create('logger3', { color: Logger.Colors.Magenta, showTimestamp: false, showLevel: false });
const logger4 = Logger.create('logger4-local-time', { useLocalTime: true });

const red = Logger.create('red', { color: Logger.Colors.Red, showTimestamp: false, showLevel: false });
const green = Logger.create('green', { color: Logger.Colors.Green, showTimestamp: false, showLevel: false });
Expand Down Expand Up @@ -37,6 +38,11 @@ logger3.info(`This is an info message #${number}`);
logger3.warn(`This is a warning message #${number}`);
logger3.error(`This is an error message #${number}`);

logger4.debug(`This is a debug message #${number}`);
logger4.info(`This is an info message #${number}`);
logger4.warn(`This is a warning message #${number}`);
logger4.error(`This is an error message #${number}`);

red.log(`Red log message`); // log() is an alias for debug()
green.log(`Green log message`);
yellow.log(`Yellow log message`);
Expand Down
6 changes: 6 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
var logger1 = Logger.create('daemon', { useColors: false });
var logger2 = Logger.create('utils');
var logger3 = Logger.create('logger3', { color: Logger.Colors.Magenta, showTimestamp: false, showLevel: false });
var logger4 = Logger.create('logger4', { useLocalTime: true });

logger1.debug(`This is a log message`);
logger1.info(`This is a log message`);
Expand All @@ -24,6 +25,11 @@
logger3.warn(`This is a log message`);
logger3.error(`This is a log message`);

logger4.debug(`This is a log message`);
logger4.info(`This is a log message`);
logger4.warn(`This is a log message`);
logger4.error(`This is a log message`);

var red = Logger.create('red', { color: Logger.Colors.Red, showTimestamp: false, showLevel: false });
var green = Logger.create('green', { color: Logger.Colors.Green, showTimestamp: false, showLevel: false });
var yellow = Logger.create('yellow', { color: Logger.Colors.Yellow, showTimestamp: false, showLevel: false });
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const defaultOptions = {
useColors: true,
color: Colors.Default,
showTimestamp: true,
useLocalTime: false,
showLevel: true,
filename: GlobalLogfile,
appendFile: true,
Expand Down Expand Up @@ -194,9 +195,12 @@ class Logger {

let result = '';

if(this.options.showTimestamp)
if(this.options.showTimestamp && !this.options.useLocalTime)
result += '' + new Date().toISOString() + ' ';

if(this.options.showTimestamp && this.options.useLocalTime)
result += '' + new Date().toLocaleString() + ' ';

result = timestampFormat + result;

if(this.options.showLevel)
Expand Down
26 changes: 25 additions & 1 deletion test/logplease.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('logplease', function() {
useColors: false,
color: Logger.Colors.Yellow,
showTimestamp: false,
useLocalTime: true,
showLevel: false,
filename: 'test.log',
appendFile: false,
Expand All @@ -79,6 +80,7 @@ describe('logplease', function() {
assert.equal(log.options.useColors, false)
assert.equal(log.options.color, Logger.Colors.Yellow)
assert.equal(log.options.showTimestamp, false)
assert.equal(log.options.useLocalTime, true)
assert.equal(log.options.showLevel, false)
assert.equal(log.options.filename, 'test.log')
assert.equal(log.options.appendFile, false)
Expand All @@ -96,6 +98,7 @@ describe('logplease', function() {
assert.equal(log.options.useColors, false)
assert.equal(log.options.color, Logger.Colors.Yellow)
assert.equal(log.options.showTimestamp, true)
assert.equal(log.options.useLocalTime, false)
assert.equal(log.options.showLevel, true)
assert.equal(log.options.filename, null)
assert.equal(log.options.appendFile, true)
Expand Down Expand Up @@ -178,15 +181,36 @@ describe('logplease', function() {
done()
})

it('writes timestamp', (done) => {
it('writes timestamp in iso time', (done) => {
let out = ''
let old = console.log
// ignore seconds when comparing times
let isoTime = new Date().toISOString().slice(0, 19)
console.log = (d) => out += d
const log = Logger.create('test1')
log.debug("hi")
console.log = old
assert.equal(out.split(" ").length, 4)
assert.equal(out.split(" ")[3], 'hi')
let loggedTime = out.split(" ")[0].replace('\u001b[37m', '').slice(0, 19)
assert.equal(isoTime, loggedTime)
done()
})

it('writes timestamp in local time', (done) => {
let out = ''
let old = console.log
let localTime = new Date().toLocaleString()
console.log = (d) => out += d
const log = Logger.create('test1', {useLocalTime: true})
log.debug("hi")
console.log = old
let logArray = out.split(" ")
// extra space in local time increases length
assert.equal(logArray.length, 5)
assert.equal(logArray[4], 'hi')
let loggedTime = logArray.slice(0, 2).join(' ').replace('\u001b[37m', '')
assert.equal(localTime, loggedTime)
done()
})

Expand Down