Skip to content

Commit

Permalink
Add support for timestampFormat (#9)
Browse files Browse the repository at this point in the history
* Add support for timestampFormat

* Use older version of date-fns

* Add test for brighterscript logging format
  • Loading branch information
TwitchBronBron authored May 9, 2024
1 parent 5cdc494 commit c573f6a
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 6 deletions.
53 changes: 53 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
},
"dependencies": {
"chalk": "^4.1.2",
"date-fns": "^2.30.0",
"fs-extra": "^10.0.0",
"parse-ms": "^2.1.0",
"safe-json-stringify": "^1.2.0",
Expand Down
43 changes: 43 additions & 0 deletions src/Logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,49 @@ describe('Logger', () => {
const stamp = logger.formatTimestamp(new Date());
expect(/\d\d:\d\d:\d\d\.\d\d\d/.exec(stamp)).to.exist;
});

it('uses from original options', () => {
logger = new Logger({ timestampFormat: 'HH' });
expect(
logger.formatTimestamp(now)
).to.eql('04');
});

it('uses from parent options', () => {
logger = new Logger({ timestampFormat: 'HH' });
logger = logger.createLogger();
expect(
logger.formatTimestamp(now)
).to.eql('04');
});

it('uses the default when missing from options', () => {
logger = new Logger();
expect(
logger.formatTimestamp(now)
).to.eql(timestamp);
});

it('uses the default when deleted from options', () => {
logger = new Logger({ timestampFormat: 'HH' });
logger['options'].timestampFormat = undefined;
expect(
logger.formatTimestamp(now)
).to.eql(timestamp);
});

it('uses the default when deleted from logger itself', () => {
logger = new Logger({ timestampFormat: 'HH' });
logger.timestampFormat = undefined;
expect(
logger.formatTimestamp(now)
).to.eql(timestamp);
});

it('supports the brighterscript log format', () => {
logger.timestampFormat = 'hh:mm:ss:SSS aa';
expect(logger.formatTimestamp(now)).to.eql('04:05:06:789 AM');
});
});

describe('emit', () => {
Expand Down
29 changes: 23 additions & 6 deletions src/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as safeJsonStringify from 'safe-json-stringify';
import { serializeError } from 'serialize-error';
import { format } from 'date-fns';
import type { ChalkFunction } from 'chalk';
// eslint-disable-next-line @typescript-eslint/no-require-imports
import Chalk = require('chalk');
Expand All @@ -20,6 +21,21 @@ export class Logger {
*/
private options: LoggerOptions;

/**
* The timestamp format string. Defaults to 'HH:mm:ss.SSS' (24-hour time with milliseconds)
*
* https://date-fns.org/v2.30.0/docs/format
*/
public get timestampFormat(): string {
return this.options.timestampFormat ?? this.options.parent?.timestampFormat ?? 'HH:mm:ss.SSS';
}
public set timestampFormat(value: string | undefined) {
this.options.timestampFormat = value;
}

/**
* The log level of this logger. If a log level is not specified, it will inherit from the parent logger or default to 'log'
*/
public get logLevel(): LogLevel | LogLevelNumeric {
return this.options.logLevel ?? this.options.parent?.logLevel ?? 'log';
}
Expand Down Expand Up @@ -175,12 +191,7 @@ export class Logger {
}

public formatTimestamp(date: Date) {
return date.getHours().toString().padStart(2, '0') +
':' +
date.getMinutes().toString().padStart(2, '0') +
':' +
date.getSeconds().toString().padStart(2, '0') +
'.' + date.getMilliseconds().toString().padEnd(3, '0').substring(0, 3);
return format(date, this.timestampFormat);
}

/**
Expand Down Expand Up @@ -466,6 +477,12 @@ export enum LogLevelNumeric {
export type LogLevel = 'off' | 'error' | 'warn' | 'log' | 'info' | 'debug' | 'trace';

export interface LoggerOptions {
/**
* The timestamp format string. Defaults to 'HH:mm:ss.SSS' (24-hour time with milliseconds)
*
* https://date-fns.org/v2.30.0/docs/format
*/
timestampFormat?: string;
/**
* A prefix applied to every log entry. Appears directly after the logLevel
*/
Expand Down

0 comments on commit c573f6a

Please sign in to comment.