Skip to content

Commit

Permalink
[pinpoint-apm#117] logger builder
Browse files Browse the repository at this point in the history
  • Loading branch information
feelform committed Oct 14, 2022
1 parent bbc5895 commit 72b8a5c
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
98 changes: 98 additions & 0 deletions lib/utils/logger2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/

'use strict'

const LogLevel = require('loglevel')

class Logger {
constructor() {
this.LOG_LEVEL = LogLevel.levels
this.DEFAULT_LEVEL = this.LOG_LEVEL.SILENT
this.DEFAULT_NAME = 'default_logger'
}

static get DebugBuilder() {
return class Builder {
constructor(name) {
this.name = name
this.level = LogLevel.levels.DEBUG
}
}
}

static get InfoBuilder() {
return class Builder {
constructor(name) {
this.name = name
this.level = LogLevel.levels.INFO
}
}
}

static get WarnBuilder() {
return class Builder {
constructor(name) {
this.name = name
this.level = LogLevel.levels.WARN
}
}
}

static get ErrorBuilder() {
return class Builder {
constructor(name) {
this.name = name
this.level = LogLevel.levels.ERROR
}
}
}

init(logLevel, name) {
this.logger = logLevel.getLogger(name || this.DEFAULT_NAME)
this.logger.setLevel(logLevel || this.DEFAULT_LEVEL)
}

get log() {
if (!this.logger) {
this.init()
this.logger.info('init logger with default level')
}
return this.logger
}

debug() {
this.logger.debug.apply(null, arguments)
}

isDebug() {
if (!this.logger) {
return false
}
return this.logger.getLevel() == LogLevel.levels.DEBUG
}

info() {
this.logger.info.apply(null, arguments)
}

isInfo() {
if (!this.logger) {
return false
}
return this.logger.getLevel() == LogLevel.levels.INFO
}

warn() {
this.logger.warn.apply(null, arguments)
}

error() {
this.logger.error.apply(null, arguments)
}
}

module.exports = Logger
22 changes: 22 additions & 0 deletions test/utils/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

const test = require('tape')
const log = require('../../lib/utils/logger')
const Logger = require('../../lib/utils/logger2')
const LogLevel = require('loglevel')

test('isDebug', (t) => {
t.plan(3)
Expand All @@ -28,4 +30,24 @@ test('isInfo', (t) => {
log.init("INFO")
t.equal(log.isInfo(), true, 'info')
t.equal(log.isDebug(), false, 'debug false')
})

test('Logger.Builder', (t) => {
let actual = new Logger.DebugBuilder('debug')
t.equal(actual.name, 'debug', 'debug name match')
t.equal(actual.level, LogLevel.levels.DEBUG, 'debug name match')

actual = new Logger.InfoBuilder('info')
t.equal(actual.name, 'info', 'info name match')
t.equal(actual.level, LogLevel.levels.INFO, 'info name match')

actual = new Logger.WarnBuilder('warn')
t.equal(actual.name, 'warn', 'warn name match')
t.equal(actual.level, LogLevel.levels.WARN, 'warn name match')

actual = new Logger.ErrorBuilder('error')
t.equal(actual.name, 'error', 'error name match')
t.equal(actual.level, LogLevel.levels.ERROR, 'error name match')

t.end()
})

0 comments on commit 72b8a5c

Please sign in to comment.