Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for timestampFormat #9

Merged
merged 3 commits into from
May 9, 2024
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
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