Skip to content

Commit

Permalink
Support for annotating logs with prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Silic0nS0ldier committed Apr 23, 2020
1 parent e5fd095 commit 119fe68
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added
- Optional prefix annotation.

## [0.1.0] - 2020-04-23

Initial release.
Expand Down
11 changes: 6 additions & 5 deletions docs/api-extractor/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import { Logger } from 'ts-log';

// @public
export class GulpLogLogger implements Logger {
constructor(prefix?: string);
// (undocumented)
debug(message?: any, ...optionalParams: any[]): void;
debug: (message?: any, ...optionalParams: any[]) => void;
// (undocumented)
error(message?: any, ...optionalParams: any[]): void;
error: (message?: any, ...optionalParams: any[]) => void;
// (undocumented)
info(message?: any, ...optionalParams: any[]): void;
info: (message?: any, ...optionalParams: any[]) => void;
// (undocumented)
trace(message?: any, ...optionalParams: any[]): void;
trace: (message?: any, ...optionalParams: any[]) => void;
// (undocumented)
warn(message?: any, ...optionalParams: any[]): void;
warn: (message?: any, ...optionalParams: any[]) => void;
}


Expand Down
8 changes: 7 additions & 1 deletion src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ test("The lot", t => {
}

const logger = new GulpLogLogger();
const loggerAnnotated = new GulpLogLogger("pkg");

function createAssertions(logFn: (message?: any, ...optionalArgs: any[]) => void, gulpLogLevel: string, messagePrefix?: string) {
messagePrefix = messagePrefix ?? "";
Expand All @@ -32,17 +33,22 @@ test("The lot", t => {
}

// Trace
createAssertions(logger.trace, "debug", "TRACE: ");
createAssertions(logger.trace, "debug", "TRACE ");
createAssertions(loggerAnnotated.trace, "debug", "TRACE pkg: ");

// Debug
createAssertions(logger.debug, "debug");
createAssertions(loggerAnnotated.debug, "debug", "pkg: ");

// Info
createAssertions(logger.info, "info");
createAssertions(loggerAnnotated.info, "info", "pkg: ");

// Warn
createAssertions(logger.warn, "warn");
createAssertions(loggerAnnotated.warn, "warn", "pkg: ");

// Error
createAssertions(logger.error, "error");
createAssertions(loggerAnnotated.error, "error", "pkg: ");
});
47 changes: 30 additions & 17 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as GulpLog from "gulplog";
import { Logger } from "ts-log";

function log(logFn: (...args: any[]) => void, message: any, args: any[]) {
function log(prefix: string, logFn: (...args: any[]) => void, message: any, args: any[]) {
// Handle args
let processedArgs: null|string = null;
if (args.length > 0) {
Expand All @@ -15,19 +15,19 @@ function log(logFn: (...args: any[]) => void, message: any, args: any[]) {
}

// Handle message
let processedMessage: string;
let processedMessage: string = prefix;
if (typeof message === "string") {
// All is well
processedMessage = message;
processedMessage += message;
} else if (typeof message === "undefined") {
// Make it clear the message is undefined
processedMessage = "(undefined)";
processedMessage += "(undefined)";
} else if (message === null) {
// Make it clear the message is null
processedMessage = "(null)";
processedMessage += "(null)";
} else {
// Final case, stringify it
processedMessage = JSON.stringify(message);
processedMessage += JSON.stringify(message);
}

if (processedArgs) {
Expand All @@ -44,23 +44,36 @@ function log(logFn: (...args: any[]) => void, message: any, args: any[]) {
* @public
*/
export class GulpLogLogger implements Logger {
trace(message?: any, ...optionalParams: any[]): void {
private prefix: string;

/**
* @param prefix - Optionally annotate logs with a prefix such as the package name to identify log source.
*/
constructor(prefix?: string) {
if (prefix) {
this.prefix = `${prefix}: `;
} else {
this.prefix = "";
}
}

trace = (message?: any, ...optionalParams: any[]): void => {
// Trace doesn't exist in gulplog so we need to note the actual log level and throw into debug
function traceDecorate(message: any, ...args: any[]) {
GulpLog.debug(`TRACE: ${message}`, ...args);
GulpLog.debug(`TRACE ${message}`, ...args);
}
log(traceDecorate, message, optionalParams);
log(this.prefix, traceDecorate, message, optionalParams);
}
debug(message?: any, ...optionalParams: any[]): void {
log(GulpLog.debug, message, optionalParams);
debug = (message?: any, ...optionalParams: any[]): void => {
log(this.prefix, GulpLog.debug, message, optionalParams);
}
info(message?: any, ...optionalParams: any[]): void {
log(GulpLog.info, message, optionalParams);
info = (message?: any, ...optionalParams: any[]): void => {
log(this.prefix, GulpLog.info, message, optionalParams);
}
warn(message?: any, ...optionalParams: any[]): void {
log(GulpLog.warn, message, optionalParams);
warn = (message?: any, ...optionalParams: any[]): void => {
log(this.prefix, GulpLog.warn, message, optionalParams);
}
error(message?: any, ...optionalParams: any[]): void {
log(GulpLog.error, message, optionalParams);
error = (message?: any, ...optionalParams: any[]): void => {
log(this.prefix, GulpLog.error, message, optionalParams);
}
}

0 comments on commit 119fe68

Please sign in to comment.