Skip to content

Commit

Permalink
Fix bug where multiple copies of logger breaks logging errors (webdri…
Browse files Browse the repository at this point in the history
…verio#4785)

* Reproduce bug where we replace method factory more than once

* Source 2nd copy of code instead from build dir

* Use the extra logger to make the no-unused-vars rule happy

* Only extend methodFactory if it's the first time

* Explicitly copy the source, rather than relying on the compile step to do it

* Exclude build dir from coverage, go back to using it as 2nd source

* Switch to solution with fewest non-whitespace changes

* Prefix children rather than root logger
  • Loading branch information
johnnymo87 authored and christian-bromann committed Nov 26, 2019
1 parent ae06785 commit 72ce73e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -124,7 +124,8 @@
"packages/wdio-devtools-service/src/scripts",
"packages/webdriverio/build",
"packages/webdriver/build",
"packages/wdio-cucumber-framework/tests/fixtures"
"packages/wdio-cucumber-framework/tests/fixtures",
"packages/wdio-logger/build"
]
},
"greenkeeper": {
Expand Down
18 changes: 9 additions & 9 deletions packages/wdio-logger/src/node.js
Expand Up @@ -48,13 +48,13 @@ const SERIALIZERS = [{
serialize: (log) => chalk.cyan(log)
}]

const loggers = {}
const loggers = log.getLoggers()
let logLevelsConfig = {}
const logCache = new Set()
let logFile

const originalFactory = log.methodFactory
log.methodFactory = function (methodName, logLevel, loggerName) {
const wdioLoggerMethodFactory = function (methodName, logLevel, loggerName) {
const rawMethod = originalFactory(methodName, logLevel, loggerName)
return (...args) => {
/**
Expand Down Expand Up @@ -101,13 +101,6 @@ log.methodFactory = function (methodName, logLevel, loggerName) {
}
}

prefix.apply(log, {
template: '%t %l %n:',
timestampFormatter: (date) => chalk.gray(date.toISOString()),
levelFormatter: (level) => chalk[COLORS[level]](level.toUpperCase()),
nameFormatter: (name) => chalk.whiteBright(name)
})

export default function getLogger (name) {
/**
* check if logger was already initiated
Expand All @@ -124,6 +117,13 @@ export default function getLogger (name) {

loggers[name] = log.getLogger(name)
loggers[name].setLevel(logLevel)
loggers[name].methodFactory = wdioLoggerMethodFactory
prefix.apply(loggers[name], {
template: '%t %l %n:',
timestampFormatter: (date) => chalk.gray(date.toISOString()),
levelFormatter: (level) => chalk[COLORS[level]](level.toUpperCase()),
nameFormatter: (name) => chalk.whiteBright(name)
})
return loggers[name]
}
/**
Expand Down
14 changes: 14 additions & 0 deletions packages/wdio-logger/tests/node.test.js
@@ -1,5 +1,6 @@
import fs from 'fs'
import nodeLogger from '../src/node'
import nodeLogger2 from '../build/node'

describe('wdio-logger node', () => {
describe('log level', () => {
Expand Down Expand Up @@ -200,6 +201,19 @@ describe('wdio-logger node', () => {
expect(write.mock.results[3].value).toContain('test-logFile4: Error: bar')
})

it('is not confused by multiple copies of source code', () => {
process.env.WDIO_LOG_PATH = 'wdio.test.log'

const log = nodeLogger('test-logFile4')
const log2 = nodeLogger2('test-logFile4')
log.info('foo')
log2.error(new Error('bar'))

expect(write.mock.calls.length).toBe(2)
expect(write.mock.results[0].value).toContain('test-logFile4: foo')
expect(write.mock.results[1].value).toContain('test-logFile4: Error: bar')
})

describe('waitForBuffer with logFile', () => {
const scenarios = [{
name: 'should be ok buffer is empty',
Expand Down

0 comments on commit 72ce73e

Please sign in to comment.