Skip to content

Commit

Permalink
Merge pull request #5 from SecJS/refactor/len-default-log-mapper
Browse files Browse the repository at this point in the history
refactor: Add default log mapper to Log and Logger
  • Loading branch information
jlenon7 authored Oct 31, 2021
2 parents 2369c10 + 37cf282 commit 842e1a5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@secjs/logger",
"version": "1.1.5",
"version": "1.1.6",
"description": "",
"scripts": {
"build": "tsc",
Expand Down
14 changes: 6 additions & 8 deletions src/Logger/Log.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { LogMapper } from './LogMapper'
import { ContextFormatter } from '../Formatters/ContextFormatter'
import { ConsoleTransporter } from '../Transporters/ConsoleTransporter'
import { defaultMapper } from './defaultMapper'

let mapper = new LogMapper(
[new ContextFormatter('Log')],
[new ConsoleTransporter('stdout')],
)
let mapper = defaultMapper

export function Log(
message: any,
formatterOpts?: any,
transporterOpts?: any,
formatterOpts: any = {},
transporterOpts: any = {},
): void {
formatterOpts.context = 'Log'

mapper.resolve(message, formatterOpts, transporterOpts)
}

Expand Down
12 changes: 2 additions & 10 deletions src/Logger/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { Color } from '../utils/Color'
import { LogMapper } from './LogMapper'
import { ContextFormatter } from '../Formatters/ContextFormatter'
import { FileTransporter } from '../Transporters/FileTransporter'
import { ConsoleTransporter } from '../Transporters/ConsoleTransporter'
import { defaultMapper } from './defaultMapper'

export class Logger {
private readonly context: string
private readonly mapper: LogMapper

constructor(context = Logger.name, mapper?: LogMapper) {
this.context = context
this.mapper =
mapper ||
new LogMapper(
[new ContextFormatter(context)],
[new ConsoleTransporter(), new FileTransporter()],
)
this.mapper = mapper || defaultMapper
}

info(message: any, formatterOpts: any = {}, transporterOpts: any = {}) {
Expand All @@ -24,7 +17,6 @@ export class Logger {
formatterOpts.context = formatterOpts.context || this.context
transporterOpts.streamType = 'stdout'

this.mapper.removeTransporter(FileTransporter)
this.mapper.resolve(message, formatterOpts, transporterOpts)
}

Expand Down
8 changes: 8 additions & 0 deletions src/Logger/defaultMapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { LogMapper } from './LogMapper'
import { ContextFormatter } from '../Formatters/ContextFormatter'
import { ConsoleTransporter } from '../Transporters/ConsoleTransporter'

export const defaultMapper = new LogMapper(
[new ContextFormatter()],
[new ConsoleTransporter()],
)

0 comments on commit 842e1a5

Please sign in to comment.