-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0692c28
commit 092d7e7
Showing
16 changed files
with
242 additions
and
16 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
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
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
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,55 @@ | ||
import { inject, injectable, multiInject, optional } from 'inversify'; | ||
import { ILogMessage, ILogService, ILogSink, ILogSinkSymbol } from 'ipmc-interfaces'; | ||
|
||
@injectable() | ||
export class LogService implements ILogService { | ||
public constructor( | ||
@multiInject(ILogSinkSymbol) @optional() private readonly sinks: ILogSink[] | ||
) { } | ||
|
||
public trace(msg: string): void { | ||
this.writeMessage({ | ||
message: msg, | ||
level: 'TRACE', | ||
}); | ||
} | ||
|
||
public debug(msg: string): void { | ||
this.writeMessage({ | ||
message: msg, | ||
level: 'DEBUG', | ||
}); | ||
} | ||
|
||
public info(msg: string): void { | ||
this.writeMessage({ | ||
message: msg, | ||
level: 'INFO', | ||
}); | ||
} | ||
|
||
public warn(msg: string): void { | ||
this.writeMessage({ | ||
message: msg, | ||
level: 'WARN', | ||
}); | ||
} | ||
|
||
public error(msg: Error | string): void { | ||
if (typeof msg === 'string') { | ||
this.writeMessage({ | ||
message: msg, | ||
level: 'ERROR', | ||
}); | ||
} else { | ||
this.writeMessage({ | ||
message: `${msg.name}: ${msg.message} | ${msg.cause} | ${msg.stack}`, | ||
level: 'ERROR', | ||
}); | ||
} | ||
} | ||
|
||
private writeMessage(msg: Omit<ILogMessage, 'time'>) { | ||
this.sinks.forEach((sink) => sink.write({ ...msg, time: new Date(Date.now()) })); | ||
} | ||
} |
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,11 @@ | ||
import { injectable } from 'inversify'; | ||
import { ILogMessage, ILogSink } from 'ipmc-interfaces'; | ||
|
||
@injectable() | ||
export class MemoryLogSink implements ILogSink { | ||
public write(msg: ILogMessage): void { | ||
this.logs.push(msg); | ||
} | ||
|
||
public logs: ILogMessage[] = []; | ||
} |
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
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
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,52 @@ | ||
import { ILogService, ILogServiceSymbol, ILogSinkSymbol } from 'ipmc-interfaces'; | ||
import { describe, expect, test } from 'vitest'; | ||
import { Application, LogService, MemoryLogSink } from '../../src'; | ||
|
||
describe('LogService', () => { | ||
const app = new Application(); | ||
app.register(LogService, ILogServiceSymbol); | ||
app.register(MemoryLogSink, ILogSinkSymbol); | ||
|
||
test('can log a info message', () => { | ||
const log = app.getService<ILogService>(ILogServiceSymbol)!; | ||
const sink = app.getService<MemoryLogSink>(ILogSinkSymbol)!; | ||
sink.logs = []; | ||
|
||
log.info('this is a test'); | ||
|
||
expect(sink.logs.length).toBe(1); | ||
expect(sink.logs[0].message).toBe('this is a test'); | ||
expect(sink.logs[0].level).toBe('INFO'); | ||
}); | ||
|
||
test('can log a warning message', () => { | ||
const log = app.getService<ILogService>(ILogServiceSymbol)!; | ||
const sink = app.getService<MemoryLogSink>(ILogSinkSymbol)!; | ||
sink.logs = []; | ||
|
||
log.warn('this is a test'); | ||
|
||
expect(sink.logs.length).toBe(1); | ||
expect(sink.logs[0].message).toBe('this is a test'); | ||
expect(sink.logs[0].level).toBe('WARN'); | ||
}); | ||
|
||
test('can log a error message', () => { | ||
const log = app.getService<ILogService>(ILogServiceSymbol)!; | ||
const sink = app.getService<MemoryLogSink>(ILogSinkSymbol)!; | ||
sink.logs = []; | ||
|
||
log.error('this is a test'); | ||
|
||
expect(sink.logs.length).toBe(1); | ||
expect(sink.logs[0].message).toBe('this is a test'); | ||
expect(sink.logs[0].level).toBe('ERROR'); | ||
|
||
const error = new Error('TestError'); | ||
log.error(error); | ||
|
||
expect(sink.logs.length).toBe(2); | ||
expect(sink.logs[1].message.startsWith('Error: TestError')).toBe(true); | ||
expect(sink.logs[1].level).toBe('ERROR'); | ||
}); | ||
}); |
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
26 changes: 26 additions & 0 deletions
26
packages/interfaces/src/Services/ILogService/ILogMessage.ts
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,26 @@ | ||
/** | ||
* Contains various bits of a log entry. | ||
*/ | ||
export interface ILogMessage { | ||
/** | ||
* The message of the log entry. | ||
*/ | ||
message: string; | ||
|
||
/** | ||
* The time of the log entry. | ||
*/ | ||
time: Date; | ||
|
||
/** | ||
* The selected log level. | ||
*/ | ||
level: TLogLevel; | ||
|
||
/** | ||
* Attached error if any. | ||
*/ | ||
error?: Error; | ||
} | ||
|
||
export type TLogLevel = 'TRACE' | 'DEBUG' | 'INFO' | 'WARN' | 'ERROR'; |
36 changes: 36 additions & 0 deletions
36
packages/interfaces/src/Services/ILogService/ILogService.ts
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,36 @@ | ||
export const ILogServiceSymbol = Symbol.for('ILogService'); | ||
|
||
/** | ||
* A service to write logs. | ||
*/ | ||
export interface ILogService { | ||
/** | ||
* Writes a trace message to the log. | ||
* @param msg message to write. | ||
*/ | ||
trace(msg: string): void; | ||
|
||
/** | ||
* Writes a debug message to the log. | ||
* @param msg message to write. | ||
*/ | ||
debug(msg: string): void; | ||
|
||
/** | ||
* Writes a info message to the log. | ||
* @param msg message to write. | ||
*/ | ||
info(msg: string): void; | ||
|
||
/** | ||
* Writes a warning message to the log. | ||
* @param msg message to write. | ||
*/ | ||
warn(msg: string): void; | ||
|
||
/** | ||
* Writes a error message to the log. | ||
* @param msg message to write. | ||
*/ | ||
error(msg: Error | string): void; | ||
} |
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,14 @@ | ||
import { ILogMessage } from './ILogMessage'; | ||
|
||
export const ILogSinkSymbol = Symbol.for('ILogSink'); | ||
|
||
/** | ||
* A log sink to write the messages to. | ||
*/ | ||
export interface ILogSink { | ||
/** | ||
* Write a message to the sink. | ||
* @param msg the message to write to the sink. | ||
*/ | ||
write(msg: ILogMessage): void; | ||
} |
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,3 @@ | ||
export { type ILogMessage } from './ILogMessage'; | ||
export { type ILogService, ILogServiceSymbol } from './ILogService'; | ||
export { type ILogSink, ILogSinkSymbol } from './ILogSink'; |
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
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,9 @@ | ||
import { injectable } from 'inversify'; | ||
import { ILogMessage, ILogSink } from 'ipmc-interfaces'; | ||
|
||
@injectable() | ||
export class ConsoleLogSink implements ILogSink { | ||
write(msg: ILogMessage): void { | ||
console.log(`[${msg.time.toISOString()}][${msg.level}]: ${msg.message}`, msg.error); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import { IModule } from 'ipmc-core'; | ||
import { IDialogServiceSymbol, IPopupServiceSymbol } from 'ipmc-interfaces'; | ||
import { IDialogServiceSymbol, ILogSinkSymbol, IPopupServiceSymbol } from 'ipmc-interfaces'; | ||
import { DialogService } from './DialogService'; | ||
import { PopupService } from './PopupService'; | ||
import { ThemeService, ThemeServiceSymbol } from './ThemeService'; | ||
import { AppbarButtonService, AppbarButtonServiceSymbol } from './AppbarButtonService'; | ||
import { ConsoleLogSink } from './ConsoleLogSink'; | ||
|
||
export const UiModule: IModule = (app) => { | ||
app.register(PopupService, IPopupServiceSymbol); | ||
app.register(DialogService, IDialogServiceSymbol); | ||
app.register(ThemeService, ThemeServiceSymbol); | ||
app.register(AppbarButtonService, AppbarButtonServiceSymbol); | ||
app.registerMultiple(ConsoleLogSink, ILogSinkSymbol); | ||
}; |