-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Douglas Gubert <[email protected]>
- Loading branch information
1 parent
1e36587
commit 1e4839b
Showing
17 changed files
with
440 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import * as stackTrace from 'npm:stack-trace' | ||
import { StackFrame } from 'npm:stack-trace' | ||
|
||
enum LogMessageSeverity { | ||
DEBUG = 'debug', | ||
INFORMATION = 'info', | ||
LOG = 'log', | ||
WARNING = 'warning', | ||
ERROR = 'error', | ||
SUCCESS = 'success', | ||
} | ||
|
||
type Entry = { | ||
caller: string; | ||
severity: LogMessageSeverity; | ||
method: string; | ||
timestamp: Date; | ||
args: Array<unknown>; | ||
} | ||
|
||
interface ILoggerStorageEntry { | ||
appId: string; | ||
method: string; | ||
entries: Array<Entry>; | ||
startTime: Date; | ||
endTime: Date; | ||
totalTime: number; | ||
_createdAt: Date; | ||
} | ||
|
||
export class Logger { | ||
private appId: string; | ||
private entries: Array<Entry>; | ||
private start: Date; | ||
private method: string; | ||
|
||
constructor(method: string, appId: string) { | ||
this.appId = appId; | ||
this.method = method; | ||
this.entries = []; | ||
this.start = new Date(); | ||
} | ||
|
||
public debug(...args: Array<unknown>): void { | ||
this.addEntry(LogMessageSeverity.DEBUG, this.getStack(stackTrace.get()), ...args) | ||
} | ||
|
||
public info(...args: Array<unknown>): void { | ||
this.addEntry(LogMessageSeverity.INFORMATION, this.getStack(stackTrace.get()), ...args) | ||
} | ||
|
||
public log(...args: Array<unknown>): void { | ||
this.addEntry(LogMessageSeverity.LOG, this.getStack(stackTrace.get()), ...args) | ||
} | ||
|
||
public warning(...args: Array<unknown>): void { | ||
this.addEntry(LogMessageSeverity.WARNING, this.getStack(stackTrace.get()), ...args) | ||
} | ||
|
||
public error(...args: Array<unknown>): void { | ||
this.addEntry(LogMessageSeverity.ERROR, this.getStack(stackTrace.get()), ...args) | ||
} | ||
|
||
public success(...args: Array<unknown>): void { | ||
this.addEntry(LogMessageSeverity.SUCCESS, this.getStack(stackTrace.get()), ...args) | ||
} | ||
|
||
private addEntry(severity: LogMessageSeverity, caller: string, ...items: Array<unknown>): void { | ||
const i = items.map((args) => { | ||
if (args instanceof Error) { | ||
return JSON.stringify(args, Object.getOwnPropertyNames(args)); | ||
} | ||
if (typeof args === 'object' && args !== null && 'stack' in args) { | ||
return JSON.stringify(args, Object.getOwnPropertyNames(args)); | ||
} | ||
if (typeof args === 'object' && args !== null && 'message' in args) { | ||
return JSON.stringify(args, Object.getOwnPropertyNames(args)); | ||
} | ||
const str = JSON.stringify(args, null, 2); | ||
return str ? JSON.parse(str) : str; // force call toJSON to prevent circular references | ||
|
||
}); | ||
|
||
this.entries.push({ | ||
caller, | ||
severity, | ||
method: this.method, | ||
timestamp: new Date(), | ||
args: i, | ||
}); | ||
} | ||
|
||
private getStack(stack: Array<StackFrame>): string { | ||
let func = 'anonymous'; | ||
|
||
if (stack.length === 1) { | ||
return func; | ||
} | ||
|
||
const frame = stack[1]; | ||
|
||
if (frame.getMethodName() === null) { | ||
func = 'anonymous OR constructor'; | ||
} else { | ||
func = frame.getMethodName(); | ||
} | ||
|
||
if (frame.getFunctionName() !== null) { | ||
func = `${func} -> ${frame.getFunctionName()}`; | ||
} | ||
|
||
return func; | ||
} | ||
|
||
private getTotalTime(): number { | ||
return new Date().getTime() - this.start.getTime(); | ||
} | ||
|
||
public hasEntries(): boolean { | ||
return this.entries.length > 0; | ||
} | ||
|
||
public getLogs(): ILoggerStorageEntry { | ||
return { | ||
appId: this.appId, | ||
method: this.method, | ||
entries: this.entries, | ||
startTime: this.start, | ||
endTime: new Date(), | ||
totalTime: this.getTotalTime(), | ||
_createdAt: new Date(), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.