diff --git a/package.json b/package.json index 7ebdb8d7..cf33a185 100644 --- a/package.json +++ b/package.json @@ -42,12 +42,13 @@ "dependencies": { "chalk": "^2.3.2", "figures": "^2.0.0", + "moment-timezone": "^0.5.31", "pkg-conf": "^2.1.0" }, "devDependencies": { "@types/node": "^11.11.3", "typescript": "^3.3.3333", - "xo": "*" + "xo": "^0.24.0" }, "options": { "default": { @@ -55,13 +56,17 @@ "displayBadge": true, "displayDate": false, "displayFilename": false, + "displayLine": true, "displayLabel": true, "displayTimestamp": false, "underlineLabel": true, "underlineMessage": false, "underlinePrefix": false, "underlineSuffix": false, - "uppercaseLabel": false + "uppercaseLabel": false, + "timeZone": "", + "formatDate": "YYYY-MM-DD", + "formatTime": "HH:mm:ss a" } }, "xo": { diff --git a/readme.md b/readme.md index 93f9c332..41c6d09b 100644 --- a/readme.md +++ b/readme.md @@ -11,16 +11,17 @@
## Description + Hackable and configurable to the core, signale can be used for logging purposes, status reporting, as well as for handling the output rendering process of other node modules and applications. Read this document in: [简体中文](https://github.com/klaussinani/signale/blob/master/docs/readme.zh_CN.md). @@ -449,13 +450,17 @@ The following illustrates all the available options with their respective defaul "displayBadge": true, "displayDate": false, "displayFilename": false, + "displayLine": false, "displayLabel": true, "displayTimestamp": false, "underlineLabel": true, "underlineMessage": false, "underlinePrefix": false, "underlineSuffix": false, - "uppercaseLabel": false + "uppercaseLabel": false, + "timeZone": "America/Argentina/Buenos_Aires", + "formatDate": "YYYY-MM-DD", + "formatTime": "HH:mm:ss" } } ``` @@ -491,6 +496,13 @@ Display the current local date in `YYYY-MM-DD` format. Display the name of the file that the logger is reporting from. +##### `displayLine` + +- Type: `Boolean` +- Default: `true` + +Display the line of the file that the logger is reporting from. + ##### `displayLabel` - Type: `Boolean` @@ -540,6 +552,27 @@ Underline the logger suffix. Display the label of the logger in uppercase. +##### `timeZone` + +- Type: `String` +- Default: `America/Argentina/Buenos_Aires` + +Time zone configuration. [List of time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List) + +##### `formatDate` + +- Type: `String` +- Default: `YYYY-MM-DD` + +Setting to format the date. [List of formats](https://momentjs.com/docs/#/displaying/format/) + +##### `formatTime` + +- Type: `String` +- Default: `HH:mm:ss a` + +Setting to format the time. [List of formats](https://momentjs.com/docs/#/displaying/format/) + ### Local diff --git a/src/signale.js b/src/signale.js index 93960c3a..4866c3c2 100644 --- a/src/signale.js +++ b/src/signale.js @@ -3,6 +3,7 @@ const util = require('util'); const path = require('path'); const readline = require('readline'); const chalk = require('chalk'); +const moment = require('moment-timezone'); const figures = require('figures'); const pkgConf = require('pkg-conf'); const pkg = require('./../package.json'); @@ -55,13 +56,11 @@ class Signale { } get date() { - const _ = new Date(); - return [_.getFullYear(), _.getMonth() + 1, _.getDate()].join('-'); + return moment().tz(this._config.timeZone).format(this._config.formatDate); } get timestamp() { - const _ = new Date(); - return [_.getHours(), _.getMinutes(), _.getSeconds()].join(':'); + return moment().tz(this._config.timeZone).format(this._config.formatTime); } get filename() { @@ -79,6 +78,27 @@ class Signale { return firstExternalFilePath ? path.basename(firstExternalFilePath) : 'anonymous'; } + get fileLine() { + const _ = Error.prepareStackTrace; + Error.prepareStackTrace = (error, stack) => stack; + const {stack} = new Error(); + Error.prepareStackTrace = _; + + const callers = stack.map(x => x.getFileName()); + const callersAndLines = stack.map(x => { + return { + name: x.getFileName(), + line: x.getLineNumber() + }; + }); + + const firstExternalFileLine = callersAndLines.find(x => { + return x.name !== callers[0]; + }); + + return firstExternalFileLine ? firstExternalFileLine.line : ''; + } + get packageConfiguration() { return pkgConf.sync(namespace, {defaults}); } @@ -153,8 +173,8 @@ class Signale { return `[${this.date}]`; } - _formatFilename() { - return `[${this.filename}]`; + _formatFilename(displayLine) { + return `[${this.filename}${displayLine ? `:${this.fileLine}` : ''}]`; } _formatScopeName() { @@ -186,7 +206,7 @@ class Signale { } if (this._config.displayFilename) { - meta.push(this._formatFilename()); + meta.push(this._formatFilename(this._config.displayLine)); } if (this._scopeName.length !== 0 && this._config.displayScope) { diff --git a/test/default.ts b/test/default.ts index 0137b41c..06e35ac8 100644 --- a/test/default.ts +++ b/test/default.ts @@ -2,6 +2,16 @@ import * as logger1 from '..'; const { Signale } = logger1; +logger1.config({ + displayFilename: true, + displayTimestamp: true, + displayDate: true, + displayLine: true, + timeZone: "America/Argentina/Buenos_Aires", + formatDate: "DD-MM-YYYY", + formatTime: "HH:mm:ss a" +}); + logger1.success('Operation successful'); logger1.debug('Hello', 'from', 'L59'); logger1.pending('Write release notes for %s', '1.2.0'); diff --git a/types/signale.d.ts b/types/signale.d.ts index 80993a27..748a4f83 100644 --- a/types/signale.d.ts +++ b/types/signale.d.ts @@ -62,6 +62,7 @@ declare namespace _signale { displayBadge?: boolean; displayDate?: boolean; displayFilename?: boolean; + displayLine?: boolean; displayLabel?: boolean; displayScope?: boolean; displayTimestamp?: boolean; @@ -70,6 +71,9 @@ declare namespace _signale { underlinePrefix?: boolean; underlineSuffix?: boolean; uppercaseLabel?: boolean; + timeZone?: string; + formatDate?: string; + formatTime?: string; } export interface ConstructorOptions