Skip to content

Commit

Permalink
[pinpoint-apm#117] The Logger must implements pinpoint.json config lo…
Browse files Browse the repository at this point in the history
…ading status and a log level.

loading status and log level are implementing by adaptor pattern
  • Loading branch information
feelform committed Oct 18, 2022
1 parent d752939 commit 0a92515
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 108 deletions.
30 changes: 23 additions & 7 deletions lib/supports.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@
* Apache License v2.0
*/

'use strict'
'use strict'

function getLog() {

}
const Logger = require('./utils/log/logger2')

module.exports = {
getLog: getLog
}
const log = new Logger.NoneBuilder({
logs: [],
debug: function (message) {
this.logs.push(message)
},
info: function (message) {
this.logs.push(message)
},
warn: function (message) {
this.logs.push(message)
},
error: function (message) {
this.logs.push(message)
}
}).build()

module.exports = {
getLog: function () {
return log
}
}
12 changes: 6 additions & 6 deletions lib/utils/log/logger-output-adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ class LoggerOutputAdaptor {
}

debug(message) {
if (!this.output || typeof this.output.debug != 'function') {
console.error("The Adaptor doesn't has the debug function.")
if (!this.output || typeof this.output.debug != 'function' || typeof this.output.error != 'function') {
this.output.error("The Adaptor doesn't has the debug function.")
return
}
this.output.debug(message)
}

info(message) {
if (!this.output || typeof this.output.info != 'function') {
console.error("The Adaptor doesn't has the info function.")
if (!this.output || typeof this.output.info != 'function' || typeof this.output.error != 'function') {
this.output.error("The Adaptor doesn't has the info function.")
return
}
this.output.info(message)
}

warn(message) {
if (!this.output || typeof this.output.warn != 'function') {
console.error("The Adaptor doesn't has the warn function.")
if (!this.output || typeof this.output.warn != 'function' || typeof this.output.error != 'function') {
this.output.error("The Adaptor doesn't has the warn function.")
return
}
this.output.warn(message)
Expand Down
126 changes: 67 additions & 59 deletions lib/utils/log/logger2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,99 +6,107 @@

'use strict'

const LogLevel = require('loglevel')
const LoggerOutputAdaptor = require('./logger-output-adaptor')

class Builder {
constructor(adaptor) {

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

constructor(type, adaptor) {
this.type = type
this.adaptor = new LoggerOutputAdaptor(adaptor)
}

static get DebugBuilder() {
return Logger.Builder(LogType.debug)
}

static Builder(type) {
return class Builder {
constructor(name, adaptor) {
this.name = name
this.level = LogLevel.levels.DEBUG
constructor(adaptor) {
this.type = type
this.adaptor = adaptor
}

build() {
return new Logger(this.type, this.adaptor)
}
}
}

static get InfoBuilder() {
return class Builder {
constructor(name) {
this.name = name
this.level = LogLevel.levels.INFO
}
}
return Logger.Builder(LogType.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
}
}
return Logger.Builder(LogType.warn)
}

init(logLevel, name) {
this.logger = logLevel.getLogger(name || this.DEFAULT_NAME)
this.logger.setLevel(logLevel || this.DEFAULT_LEVEL)
static get ErrorBuilder() {
return Logger.Builder(LogType.error)
}

get log() {
if (!this.logger) {
this.init()
this.logger.info('init logger with default level')
}
return this.logger
static get NoneBuilder() {
return Logger.Builder(LogType.noneConfig)
}

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

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

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

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

warn(message) {
this.adaptor.warn(message)
}

error(message) {
this.adaptor.error(message)
}
}

class LogType {

static get debug() {
return new LogType('debug')
}

static get info() {
return new LogType('info')
}

warn() {
this.logger.warn.apply(null, arguments)
static get warn() {
return new LogType('warn')
}

error() {
this.logger.error.apply(null, arguments)
static get error() {
return new LogType('error')
}

static get noneConfig() {
return new LogType('none')
}

constructor(name) {
this.name = name
}

isDebug() {
return this.name === LogType.debug.name
}

isInfo() {
return this.name === LogType.info.name
}
}


module.exports = Logger
40 changes: 26 additions & 14 deletions test/utils/log/logger-output-adaptor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,63 @@ const test = require('tape')
const LoggerOutputAdaptor = require('../../../lib/utils/log/logger-output-adaptor')

test('Adaptor method duck typing validation', (t) => {
let actual = new LoggerOutputAdaptor({
const error = {
error: function () {
}
}

let cloneError = Object.assign({}, error)
let actual = new LoggerOutputAdaptor(Object.assign(cloneError, {
debug: function (message) {
this.actualMessage = message
}
})
}))
actual.debug('message')
t.equal(actual.output.actualMessage, 'message')

let actualUnsafe = new LoggerOutputAdaptor({
})
cloneError = Object.assign({}, error)
let actualUnsafe = new LoggerOutputAdaptor(Object.assign(cloneError, {
}))
actualUnsafe.debug('message')
t.false(actualUnsafe.output.debug, 'this.output.debug is undefined')


actual = new LoggerOutputAdaptor({
cloneError = Object.assign({}, error)
actual = new LoggerOutputAdaptor(Object.assign(cloneError, {
info: function (message) {
this.actualMessage = message
}
})
}))
actual.info('message2')
t.equal(actual.output.actualMessage, 'message2')

actualUnsafe = new LoggerOutputAdaptor({
})
cloneError = Object.assign({}, error)
actualUnsafe = new LoggerOutputAdaptor(Object.assign(cloneError, {
}))
actualUnsafe.info('message')
t.false(actualUnsafe.output.info, 'this.output.info is undefined')

actual = new LoggerOutputAdaptor({
cloneError = Object.assign({}, error)
actual = new LoggerOutputAdaptor(Object.assign(cloneError, {
warn: function (message) {
this.actualMessage = message
}
})
}))
actual.warn('message3')
t.equal(actual.output.actualMessage, 'message3')

actualUnsafe = new LoggerOutputAdaptor({
})
cloneError = Object.assign({}, error)
actualUnsafe = new LoggerOutputAdaptor(Object.assign(cloneError, {
}))
actualUnsafe.warn('message')
t.false(actualUnsafe.output.warn, 'this.output.warn is undefined')

actual = new LoggerOutputAdaptor({
cloneError = Object.assign({}, error)
actual = new LoggerOutputAdaptor(Object.assign(cloneError, {
error: function (message) {
this.actualMessage = message
}
})
}))
actual.error('message4')
t.equal(actual.output.actualMessage, 'message4')

Expand Down
7 changes: 4 additions & 3 deletions test/utils/log/logger.integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
'use strict'
const test = require('tape')
const { getLog } = require('../../../lib/supports')
const { getLog } = require('../../../lib/agent')
const Agent = require('../../../lib/agent')

test('Logger Adaptor', (t) => {
const logger = getLog()
t.equal(logger.)
const actual = getLog()
new Agent()
t.equal(typeof actual.adaptor, 'LoggerOutputAdaptor', 'log adaptor')
t.end()
})
44 changes: 25 additions & 19 deletions test/utils/log/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
'use strict'

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

test('isDebug', (t) => {
t.plan(3)
Expand All @@ -33,21 +32,28 @@ test('isInfo', (t) => {
})

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')

const expectedAdaptor = {}

let actual = new Logger.DebugBuilder(expectedAdaptor).build()
t.equal(actual.type.name, 'debug', 'debug name match')
t.true(actual.adaptor.output === expectedAdaptor, 'adaptor member variable')
t.true(actual.isDebug(), 'Debug Log check')

actual = new Logger.InfoBuilder(expectedAdaptor).build()
t.equal(actual.type.name, 'info', 'info name match')
t.true(actual.adaptor.output === expectedAdaptor, 'adaptor member variable')
t.true(actual.isInfo(), 'Info log check')

actual = new Logger.WarnBuilder(expectedAdaptor).build()
t.equal(actual.type.name, 'warn', 'warn name match')
t.true(actual.adaptor.output === expectedAdaptor, 'adaptor member variable')

actual = new Logger.ErrorBuilder(expectedAdaptor).build()
t.equal(actual.type.name, 'error', 'error name match')
t.true(actual.adaptor.output === expectedAdaptor, 'adaptor member variable')

actual = new Logger.NoneBuilder(expectedAdaptor).build()
t.equal(actual.type.name, 'none', 'none name match')

t.end()
})

0 comments on commit 0a92515

Please sign in to comment.