forked from openwallet-foundation/bifold-wallet
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: normalize log api and move to dependency injection (openwallet…
…-foundation#1138) Signed-off-by: Jason C. Leach <[email protected]>
- Loading branch information
Showing
11 changed files
with
97 additions
and
147 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
This file was deleted.
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
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 |
---|---|---|
@@ -1,86 +1,62 @@ | ||
/* eslint-disable no-console,@typescript-eslint/no-explicit-any */ | ||
|
||
export enum LogLevel { | ||
test = 0, | ||
trace = 1, | ||
debug = 2, | ||
info = 3, | ||
warn = 4, | ||
error = 5, | ||
fatal = 6, | ||
off = 7, | ||
} | ||
import { BaseLogger } from '@credo-ts/core' | ||
import { consoleTransport, logger } from 'react-native-logs' | ||
|
||
export class ConsoleLogger extends BaseLogger { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
private log: any | ||
private config = { | ||
levels: { | ||
test: 0, | ||
trace: 0, | ||
debug: 0, | ||
info: 1, | ||
warn: 2, | ||
error: 3, | ||
fatal: 4, | ||
}, | ||
severity: 'debug', | ||
async: true, | ||
dateFormat: 'time', | ||
printDate: false, | ||
} | ||
|
||
export interface Logger { | ||
logLevel: LogLevel | ||
public constructor() { | ||
super() | ||
|
||
test(message: string, data?: Record<string, any>): void | ||
trace(message: string, data?: Record<string, any>): void | ||
debug(message: string, data?: Record<string, any>): void | ||
info(message: string, data?: Record<string, any>): void | ||
warn(message: string, data?: Record<string, any>): void | ||
error(message: string, data?: Record<string, any>): void | ||
fatal(message: string, data?: Record<string, any>): void | ||
} | ||
const transport = [consoleTransport] | ||
const config = { | ||
...this.config, | ||
transport, | ||
} | ||
|
||
const replaceError = (_: unknown, value: unknown) => { | ||
if (value instanceof Error) { | ||
const newValue = Object.getOwnPropertyNames(value).reduce( | ||
(obj, propName) => { | ||
obj[propName] = (value as unknown as Record<string, unknown>)[propName] | ||
return obj | ||
}, | ||
{ name: value.name } as Record<string, unknown> | ||
) | ||
return newValue | ||
this.log = logger.createLogger<'test' | 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal'>(config) | ||
} | ||
|
||
return value | ||
} | ||
|
||
export class AppConsoleLogger { | ||
public logLevel: LogLevel | ||
|
||
// Map our log levels to console levels | ||
private consoleLogMap = { | ||
[LogLevel.test]: 'log', | ||
[LogLevel.trace]: 'log', | ||
[LogLevel.debug]: 'debug', | ||
[LogLevel.info]: 'info', | ||
[LogLevel.warn]: 'warn', | ||
[LogLevel.error]: 'error', | ||
[LogLevel.fatal]: 'error', | ||
} as const | ||
public test(message: string, data?: object | undefined): void { | ||
this.log?.test(message, data) | ||
} | ||
|
||
public constructor(logLevel: LogLevel = LogLevel.off) { | ||
this.logLevel = logLevel | ||
public trace(message: string, data?: object | undefined): void { | ||
this.log?.trace(message, data) | ||
} | ||
|
||
public log(level: Exclude<LogLevel, LogLevel.off>, message: string, data?: Record<string, any>): void { | ||
// Get console method from mapping | ||
const consoleLevel = this.consoleLogMap[level] | ||
public debug(message: string, data?: object | undefined): void { | ||
this.log?.debug(message, data) | ||
} | ||
|
||
// Get logger prefix from log level names in enum | ||
const prefix = LogLevel[level].toUpperCase() | ||
public info(message: string, data?: object | undefined): void { | ||
this.log?.info(message, data) | ||
} | ||
|
||
// Return early if logging is not enabled for this level | ||
if (!this.isEnabled(level)) { | ||
console.log('logger disabled') | ||
return | ||
} | ||
public warn(message: string, data?: object | undefined): void { | ||
this.log?.warn(message, data) | ||
} | ||
|
||
// Log, with or without data | ||
if (data) { | ||
console[consoleLevel]( | ||
`${prefix}: ${new Date().toISOString()} - ${message}`, | ||
JSON.stringify(data, replaceError, 2) | ||
) | ||
} else { | ||
console[consoleLevel](`${prefix}: ${new Date().toISOString()} - ${message}`) | ||
} | ||
public error(message: string, data?: object | undefined): void { | ||
this.log?.error(message, data) | ||
} | ||
|
||
private isEnabled(logLevel: LogLevel) { | ||
return logLevel >= this.logLevel | ||
public fatal(message: string, data?: object | undefined): void { | ||
this.log?.fatal(message, data) | ||
} | ||
} |
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.