-
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.
Merge branch 'dev' into feature/ui-changes
- Loading branch information
Showing
27 changed files
with
448 additions
and
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
"type": "module", | ||
"packageManager": "[email protected]", | ||
"scripts": { | ||
"watch": "yarn workspaces foreach -Api run watch", | ||
"dev": "yarn workspaces foreach -Api run dev", | ||
"build": "yarn workspaces foreach -At run build", | ||
"test": "yarn workspaces foreach -At run test", | ||
"cli": "yarn workspace ipmc-tools run cli" | ||
|
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
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,56 @@ | ||
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}`, | ||
error: msg, | ||
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
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
Oops, something went wrong.