Skip to content

Commit

Permalink
Expose default loglevels
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt committed Dec 14, 2024
1 parent 9a5d156 commit 0e71031
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ To ensure optimal performance in production, we recommend modifying these settin

### Default log level

`tslog` comes with default log level `0: silly`, `1: trace`, `2: debug`, `3: info`, `4: warn`, `5: error`, `6: fatal`.
`tslog` comes with default log levels (`DefaultLogLevels`): `0: silly`, `1: trace`, `2: debug`, `3: info`, `4: warn`, `5: error`, `6: fatal`.

> **Tip:** Each logging method has a return type, which is a _JSON_ representation of the log message (`ILogObj`).
Expand Down
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ To ensure optimal performance in production, we recommend modifying these settin

### Default log level

`tslog` comes with default log level `0: silly`, `1: trace`, `2: debug`, `3: info`, `4: warn`, `5: error`, `6: fatal`.
`tslog` comes with default log levels (`DefaultLogLevels`): `0: silly`, `1: trace`, `2: debug`, `3: info`, `4: warn`, `5: error`, `6: fatal`.

> **Tip:** Each logging method has a return type, which is a _JSON_ representation of the log message (`ILogObj`).
Expand Down Expand Up @@ -379,7 +379,7 @@ Output:
#### minLevel

You can ignore every log message from being processed until a certain severity.
Default severities are:
Default severities (`DefaultLogLevels`) are:
`0: silly`, `1: trace`, `2: debug`, `3: info`, `4: warn`, `5: error`, `6: fatal`

```typescript
Expand Down
25 changes: 18 additions & 7 deletions src/index.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { ILogObj, ILogObjMeta, ISettingsParam } from "./interfaces.js";
export * from "./interfaces.js";
export * from "./BaseLogger.js";

export enum DefaultLogLevels {
SILLY = 0,
TRACE = 1,
DEBUG = 2,
INFO = 3,
WARN = 4,
ERROR = 5,
FATAL = 6,
}

export class Logger<LogObj> extends BaseLogger<LogObj> {
constructor(settings?: ISettingsParam<LogObj>, logObj?: LogObj) {
const isBrowser = ![typeof window, typeof document].includes("undefined");
Expand Down Expand Up @@ -34,55 +44,56 @@ export class Logger<LogObj> extends BaseLogger<LogObj> {
* @param args - Multiple log attributes that should be logged out.
*/
public silly(...args: unknown[]): (LogObj & ILogObjMeta) | undefined {
return super.log(0, "SILLY", ...args);
return super.log(DefaultLogLevels.SILLY, "SILLY", ...args);
}

/**
* Logs a trace message.
* @param args - Multiple log attributes that should be logged out.
*/
public trace(...args: unknown[]): (LogObj & ILogObjMeta) | undefined {
return super.log(1, "TRACE", ...args);
return super.log(DefaultLogLevels.TRACE, "TRACE", ...args);
}

/**
* Logs a debug message.
* @param args - Multiple log attributes that should be logged out.
*/
public debug(...args: unknown[]): (LogObj & ILogObjMeta) | undefined {
return super.log(2, "DEBUG", ...args);
return super.log(DefaultLogLevels.DEBUG, "DEBUG", ...args);
}

/**
* Logs an info message.
* @param args - Multiple log attributes that should be logged out.
*/
public info(...args: unknown[]): (LogObj & ILogObjMeta) | undefined {
return super.log(3, "INFO", ...args);
// TODO: here
return super.log(DefaultLogLevels.INFO, "INFO", ...args);
}

/**
* Logs a warn message.
* @param args - Multiple log attributes that should be logged out.
*/
public warn(...args: unknown[]): (LogObj & ILogObjMeta) | undefined {
return super.log(4, "WARN", ...args);
return super.log(DefaultLogLevels.WARN, "WARN", ...args);
}

/**
* Logs an error message.
* @param args - Multiple log attributes that should be logged out.
*/
public error(...args: unknown[]): (LogObj & ILogObjMeta) | undefined {
return super.log(5, "ERROR", ...args);
return super.log(DefaultLogLevels.ERROR, "ERROR", ...args);
}

/**
* Logs a fatal message.
* @param args - Multiple log attributes that should be logged out.
*/
public fatal(...args: unknown[]): (LogObj & ILogObjMeta) | undefined {
return super.log(6, "FATAL", ...args);
return super.log(DefaultLogLevels.FATAL, "FATAL", ...args);
}

/**
Expand Down
25 changes: 18 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ declare global {
}
}

export enum DefaultLogLevels {
SILLY = 0,
TRACE = 1,
DEBUG = 2,
INFO = 3,
WARN = 4,
ERROR = 5,
FATAL = 6,
}

export class Logger<LogObj> extends BaseLogger<LogObj> {
constructor(settings?: ISettingsParam<LogObj>, logObj?: LogObj) {
const isBrowser = typeof window !== "undefined" && typeof document !== "undefined";
Expand Down Expand Up @@ -37,55 +47,56 @@ export class Logger<LogObj> extends BaseLogger<LogObj> {
* @param args - Multiple log attributes that should be logged out.
*/
public silly(...args: unknown[]): (LogObj & ILogObjMeta) | undefined {
return super.log(0, "SILLY", ...args);
return super.log(DefaultLogLevels.SILLY, "SILLY", ...args);
}

/**
* Logs a trace message.
* @param args - Multiple log attributes that should be logged out.
*/
public trace(...args: unknown[]): (LogObj & ILogObjMeta) | undefined {
return super.log(1, "TRACE", ...args);
return super.log(DefaultLogLevels.TRACE, "TRACE", ...args);
}

/**
* Logs a debug message.
* @param args - Multiple log attributes that should be logged out.
*/
public debug(...args: unknown[]): (LogObj & ILogObjMeta) | undefined {
return super.log(2, "DEBUG", ...args);
return super.log(DefaultLogLevels.DEBUG, "DEBUG", ...args);
}

/**
* Logs an info message.
* @param args - Multiple log attributes that should be logged out.
*/
public info(...args: unknown[]): (LogObj & ILogObjMeta) | undefined {
return super.log(3, "INFO", ...args);
// TODO: here
return super.log(DefaultLogLevels.INFO, "INFO", ...args);
}

/**
* Logs a warn message.
* @param args - Multiple log attributes that should be logged out.
*/
public warn(...args: unknown[]): (LogObj & ILogObjMeta) | undefined {
return super.log(4, "WARN", ...args);
return super.log(DefaultLogLevels.WARN, "WARN", ...args);
}

/**
* Logs an error message.
* @param args - Multiple log attributes that should be logged out.
*/
public error(...args: unknown[]): (LogObj & ILogObjMeta) | undefined {
return super.log(5, "ERROR", ...args);
return super.log(DefaultLogLevels.ERROR, "ERROR", ...args);
}

/**
* Logs a fatal message.
* @param args - Multiple log attributes that should be logged out.
*/
public fatal(...args: unknown[]): (LogObj & ILogObjMeta) | undefined {
return super.log(6, "FATAL", ...args);
return super.log(DefaultLogLevels.FATAL, "FATAL", ...args);
}

/**
Expand Down

0 comments on commit 0e71031

Please sign in to comment.