Skip to content

Commit 8ad0c3d

Browse files
authored
fix: #1793 Shell adapter now respects HUBOT_LOG_LEVEL when printing message to the console (#1799)
1 parent cad58c5 commit 8ad0c3d

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

src/adapters/Shell.mjs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,26 @@ class Shell extends Adapter {
3939
super(robot)
4040
this.name = 'Shell'
4141
const levels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal']
42+
const logLevel = process.env.HUBOT_LOG_LEVEL || 'info'
43+
const levelPriorities = levels.reduce((acc, current, idx) => {
44+
acc[current] = idx
45+
return acc
46+
}, {})
47+
48+
const configuredPriority = levelPriorities[logLevel]
49+
50+
const noop = async () => {}
51+
4252
levels.forEach(level => {
43-
robot.logger[level] = async (...args) => {
44-
const color = levelColors[level] || ''
45-
const msg = `${color}[${level}]${reset} ${args.map(a => typeof a === 'object' ? JSON.stringify(a) : a).join(' ')}`
46-
await this.send({ user: { name: 'Logger', room: 'Shell' } }, msg)
53+
const priority = levelPriorities[level]
54+
if (priority >= configuredPriority) {
55+
robot.logger[level] = async (...args) => {
56+
const color = levelColors[level] || ''
57+
const msg = `${color}[${level}]${reset} ${args.map(a => typeof a === 'object' ? JSON.stringify(a) : a).join(' ')}`
58+
await this.send({ user: { name: 'Logger', room: 'Shell' } }, msg)
59+
}
60+
} else {
61+
robot.logger[level] = noop
4762
}
4863
})
4964
this.robot.on('scripts have loaded', () => {

test/Shell_test.mjs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,70 @@ describe('Shell Adapter', () => {
161161
})
162162
})
163163
})
164+
165+
describe('Shell Adapter: Print human readable logging in the console when something is logged with robot.logger', async () => {
166+
it('setting HUBOT_LOG_LEVEL to debug prints debug and info log messages to the console', async () => {
167+
process.env.HUBOT_LOG_LEVEL = 'debug'
168+
const robot = new Robot('Shell', false, 'TestHubot')
169+
await robot.loadAdapter()
170+
await robot.run()
171+
172+
const old = console.log
173+
const expected = {
174+
debug: false,
175+
info: false
176+
}
177+
console.log = (...args) => {
178+
old(...args)
179+
switch (true) {
180+
case args[0].includes('[debug]'):
181+
expected.debug = true
182+
break
183+
case args[0].includes('[info]'):
184+
expected.info = true
185+
break
186+
}
187+
}
188+
robot.logger.debug('should print debug message to console')
189+
robot.logger.info('should print info message to console')
190+
delete process.env.HUBOT_LOG_LEVEL
191+
console.log = old
192+
assert.deepEqual(expected, { debug: true, info: true })
193+
robot.shutdown()
194+
})
195+
196+
it('setting HUBOT_LOG_LEVEL to error only prints error log messages to the console', async () => {
197+
process.env.HUBOT_LOG_LEVEL = 'error'
198+
const robot = new Robot('Shell', false, 'TestHubot')
199+
await robot.loadAdapter()
200+
await robot.run()
201+
202+
const old = console.log
203+
const expected = {
204+
debug: false,
205+
info: false,
206+
error: false
207+
}
208+
console.log = (...args) => {
209+
old(...args)
210+
switch (true) {
211+
case args[0].includes('[debug]'):
212+
expected.debug = true
213+
break
214+
case args[0].includes('[info]'):
215+
expected.info = true
216+
break
217+
case args[0].includes('[error]'):
218+
expected.error = true
219+
break
220+
}
221+
}
222+
robot.logger.debug('should NOT print debug message to console')
223+
robot.logger.info('should NOT print info message to console')
224+
robot.logger.error('should print error message to console')
225+
delete process.env.HUBOT_LOG_LEVEL
226+
console.log = old
227+
assert.deepEqual(expected, { debug: false, info: false, error: true })
228+
robot.shutdown()
229+
})
230+
})

0 commit comments

Comments
 (0)