Skip to content

Commit

Permalink
Mild logger refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
islathehut committed Mar 25, 2024
1 parent aae84af commit 21f0a3b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/desktop/src/renderer/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createElectronLogger } from '@quiet/logger'
import { createQuietLogger } from '@quiet/logger'

const createLogger = createElectronLogger('desktop:renderer', true)
const createLogger = createQuietLogger('desktop:renderer', true)
export const defaultLogger = createLogger('default')
export default createLogger
40 changes: 17 additions & 23 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const consoleLogger =

export const nodeConsoleLogger = Console instanceof Function ? new Console(process.stdout, process.stderr) : console

export class ElectronLogger {
export class QuietLogger {
private isDebug: boolean

constructor(
Expand All @@ -32,42 +32,36 @@ export class ElectronLogger {
if (!this.isDebug) return

const text = this.formatMessage(message, 'debug')
nodeConsoleLogger.debug(text, ...optionalParams)
if (this.parallelConsoleLog) {
console.debug(text, ...optionalParams)
}
this.callLogMethods('debug', text, ...optionalParams)
}

error(message: any, ...optionalParams: any[]): void {
const text = this.formatMessage(message, 'error')
nodeConsoleLogger.error(text, ...optionalParams)
if (this.parallelConsoleLog) {
console.error(text, ...optionalParams)
}
this.callLogMethods('error', text, ...optionalParams)
}

info(message: any, ...optionalParams: any[]): void {
const text = this.formatMessage(message, 'info')
nodeConsoleLogger.info(text, ...optionalParams)
if (this.parallelConsoleLog) {
console.info(text, ...optionalParams)
}
this.callLogMethods('info', text, ...optionalParams)
}

log(message: any, ...optionalParams: any[]): void {
if (!this.isDebug) return
const text = this.formatMessage(message, 'log')
nodeConsoleLogger.log(text, ...optionalParams)
if (this.parallelConsoleLog) {
console.log(text, ...optionalParams)
}
this.callLogMethods('log', text, ...optionalParams)
}

warn(message: any, ...optionalParams: any[]): void {
const text = this.formatMessage(message, 'warn')
nodeConsoleLogger.warn(text, ...optionalParams)
this.callLogMethods('warn', text, ...optionalParams)
}

private callLogMethods(level: string, text: string, ...optionalParams: any[]): void {
// @ts-ignore
nodeConsoleLogger[level](text, ...optionalParams)
if (this.parallelConsoleLog) {
console.warn(text, ...optionalParams)
// @ts-ignore
console[level](text, ...optionalParams)
}
}

Expand All @@ -79,20 +73,20 @@ export class ElectronLogger {

export const electronLogger =
(packageName: string, parallelConsoleLog: boolean = false) =>
(module: string): ElectronLogger => {
(module: string): QuietLogger => {
const name = `${packageName}:${module}`
nodeConsoleLogger.info(`Initializing logger ${name}`)
return new ElectronLogger(name, parallelConsoleLog)
return new QuietLogger(name, parallelConsoleLog)
}

export const logger = (packageName: string): ((arg: string) => Logger) => {
return consoleLogger(packageName)
}

export const createElectronLogger = (
export const createQuietLogger = (
packageName: string,
parallelConsoleLog: boolean = false
): ((arg: string) => ElectronLogger) => {
): ((arg: string) => QuietLogger) => {
return electronLogger(packageName, parallelConsoleLog)
}

Expand Down
4 changes: 2 additions & 2 deletions packages/state-manager/src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { createElectronLogger } from '@quiet/logger'
import { createQuietLogger } from '@quiet/logger'

export default createElectronLogger('state-manager')
export default createQuietLogger('state-manager')

0 comments on commit 21f0a3b

Please sign in to comment.