Skip to content
This repository was archived by the owner on Jul 27, 2021. It is now read-only.

Commit 62a7742

Browse files
timneutkensgeowarin
authored andcommitted
Add time to log output (#22)
1 parent e4f2079 commit 62a7742

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
},
4242
"dependencies": {
4343
"chalk": "^1.1.3",
44-
"error-stack-parser": "^2.0.0"
44+
"error-stack-parser": "^2.0.0",
45+
"string-length": "^1.0.1"
4546
}
4647
}

src/output.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const colors = require('./utils/colors');
44
const chalk = require('chalk');
5-
const readline = require('readline')
5+
const stringLength = require('string-length');
66

77
class Debugger {
88

@@ -49,9 +49,26 @@ class Debugger {
4949

5050
title (severity, title, subtitle) {
5151
if (this.enabled) {
52+
const date = new Date();
53+
const dateString = chalk.grey(date.toLocaleTimeString());
5254
const titleFormatted = colors.formatTitle(severity, title);
5355
const subTitleFormatted = colors.formatText(severity, subtitle);
54-
this.log(titleFormatted, subTitleFormatted);
56+
const message = `${titleFormatted} ${subTitleFormatted}`
57+
58+
// In test environment we don't include timestamp
59+
if(process.env.NODE_ENV === 'test') {
60+
this.log(message);
61+
this.log();
62+
return;
63+
}
64+
65+
// Make timestamp appear at the end of the line
66+
let logSpace = process.stdout.columns - stringLength(message) - stringLength(dateString)
67+
if (logSpace <= 0) {
68+
logSpace = 10
69+
}
70+
71+
this.log(`${message}${' '.repeat(logSpace)}${dateString}`);
5572
this.log();
5673
}
5774
}

yarn.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3090,6 +3090,12 @@ string_decoder@^0.10.25, string_decoder@~0.10.x:
30903090
version "0.10.31"
30913091
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94"
30923092

3093+
string-length@^1.0.1:
3094+
version "1.0.1"
3095+
resolved "https://registry.yarnpkg.com/string-length/-/string-length-1.0.1.tgz#56970fb1c38558e9e70b728bf3de269ac45adfac"
3096+
dependencies:
3097+
strip-ansi "^3.0.0"
3098+
30933099
string-width@^1.0.1, string-width@^1.0.2:
30943100
version "1.0.2"
30953101
resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3"

0 commit comments

Comments
 (0)