Skip to content

Commit

Permalink
[pinpoint-apm#117] logger output adaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
feelform committed Oct 24, 2022
1 parent 46fff48 commit e90f76b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 45 deletions.
30 changes: 23 additions & 7 deletions lib/utils/log/logger-output-adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,49 @@ class LoggerOutputAdaptor {
this.output = output
}

get console() {
return console
}

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

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

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

error(message) {
if (!this.output || typeof this.output.error != 'function') {
console.error("The Adaptor doesn't has the warn function.")
this.console.error("The Adaptor doesn't has the warn function.")
return
}
this.output.error(message)
Expand Down
79 changes: 46 additions & 33 deletions test/utils/log/logger-output-adaptor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,69 +10,82 @@ const test = require('tape')
const LoggerOutputAdaptor = require('../../../lib/utils/log/logger-output-adaptor')

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

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

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


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

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

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

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

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

actualUnsafe = new LoggerOutputAdaptor({
t.end()
})

test('error message', (t) => {
const error = {
error: function () {
}
}
let actualUnsafe = new LoggerOutputAdaptor(Object.assign({
debug: function (message) {
this.debugMessage = message
}
}, error))
actualUnsafe.debug('debug message')
t.equal(actualUnsafe.output.debugMessage, 'debug message', 'this.output.debug is undefined')

actualUnsafe = new LoggerOutputAdaptor(Object.assign({
info: function (message) {
this.infoMessage = message
}
}, error))
actualUnsafe.info('info message')
t.equal(actualUnsafe.output.infoMessage, 'info message', 'this.output.info is undefined')

actualUnsafe = new LoggerOutputAdaptor(Object.assign({
warn: function (message) {
this.warnMessage = message
}
}, error))
actualUnsafe.warn('warn message')
t.equal(actualUnsafe.output.warnMessage, 'warn message', 'this.output.warn is undefined')

actualUnsafe = new LoggerOutputAdaptor({})
Object.defineProperty(actualUnsafe, "console", {
get: function () {
return {
error: function (message) {
actualUnsafe.errorMessage = message
}
}
}
})
actualUnsafe.error('message')
t.equal(actualUnsafe.errorMessage, 'The Adaptor doesn\'t has the warn function.', 'Adaptor function not defined error message')
t.false(actualUnsafe.output.error, 'this.output.error is undefined')

t.end()
Expand Down
18 changes: 13 additions & 5 deletions test/utils/log/logger.integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
const test = require('tape')
const { getLog } = require('../../../lib/supports')
const Agent = require('../../../lib/agent')
const { clear } = require('../../../lib/config')
const { clear, getConfig } = require('../../../lib/config')

test('Logger Adaptor', (t) => {
test('no config logger', (t) => {
clear()
let actual = getLog()
const actual = getLog()
new Agent()
t.equal(actual.adaptor.constructor.name, 'LoggerOutputAdaptor', 'log adaptor')
t.equal(actual.type.name, 'NONE', 'when first loading time, log type is NONE')
Expand All @@ -39,8 +39,16 @@ test('Logger Adaptor', (t) => {
t.equal(actual.adaptor.output.output.infoMessage, 'a info message', 'info queue message')
t.equal(actual.adaptor.output.output.warnMessage, 'a warn message', 'warn queue message')
t.equal(actual.adaptor.output.output.errorMessage, 'a error message', 'error queue message')
t.end()
})

actual = getLog()
t.equal(actual.type.name, 'WARN', 'debug')
test('config loading logger', (t) => {
clear()
getConfig()
const actual = getLog()
actual.debug('a debug message')
actual.info('a info message')
actual.warn('a warn message')
actual.error('a error message')
t.end()
})

0 comments on commit e90f76b

Please sign in to comment.