Skip to content
Open
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
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,31 @@
"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": {
"displayScope": true,
"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": {
Expand Down
41 changes: 37 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
</div>

<p align="center">
<a href="https://travis-ci.com/klaussinani/signale">
<img alt="Build Status" src="https://travis-ci.com/klaussinani/signale.svg?branch=master">
<a href="https://travis-ci.com/rudemex/signale">
<img alt="Build Status" src="https://travis-ci.org/rudemex/signale.svg?branch=master">
</a>
<a href="https://www.npmjs.com/package/signale">
<img alt="NPM Downloads" src="https://img.shields.io/npm/dt/signale.svg">
<img alt="NPM Downloads" src="https://img.shields.io/npm/dt/signale">
</a>
</p>

## 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).
Expand Down Expand Up @@ -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"
}
}
```
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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/)

</details>

### Local
Expand Down
34 changes: 27 additions & 7 deletions src/signale.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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() {
Expand All @@ -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});
}
Expand Down Expand Up @@ -153,8 +173,8 @@ class Signale {
return `[${this.date}]`;
}

_formatFilename() {
return `[${this.filename}]`;
_formatFilename(displayLine) {
return `[${this.filename}${displayLine ? `:${this.fileLine}` : ''}]`;
}

_formatScopeName() {
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions test/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 4 additions & 0 deletions types/signale.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ declare namespace _signale {
displayBadge?: boolean;
displayDate?: boolean;
displayFilename?: boolean;
displayLine?: boolean;
displayLabel?: boolean;
displayScope?: boolean;
displayTimestamp?: boolean;
Expand All @@ -70,6 +71,9 @@ declare namespace _signale {
underlinePrefix?: boolean;
underlineSuffix?: boolean;
uppercaseLabel?: boolean;
timeZone?: string;
formatDate?: string;
formatTime?: string;
}

export interface ConstructorOptions<T extends string> {
Expand Down