Skip to content

Commit

Permalink
feat: use console log for default logger and improve state resource f…
Browse files Browse the repository at this point in the history
…or logging
  • Loading branch information
meenahoda authored and Meena Brend committed Dec 1, 2021
1 parent 1fc8697 commit 4026899
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
10 changes: 6 additions & 4 deletions lib/plugin/components/services/logger/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const LEVELS = ['trace', 'debug', 'info', 'warn', 'error', 'fatal']
const nullLogger = require('abstract-logging')
const nullLogger = require('./null-logger')(LEVELS)
const pino = require('pino')
const pinoPretty = require('pino-pretty')
const moment = require('moment')
Expand Down Expand Up @@ -57,11 +57,13 @@ class Logger {
// logger.level = this.level
// return logger
} else {
const logger = nullLogger
logger.child = () => logger
return logger
return nullLogger
}
}

isValidLevel (level) {
return LEVELS.includes(level)
}
}

function generateFilePath (dirpath) {
Expand Down
8 changes: 8 additions & 0 deletions lib/plugin/components/services/logger/null-logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const logFn = level => (...args) => { console.log(`[${level}] ${args.join(' ')}`) }

module.exports = function (levels) {
const logger = {}
levels.forEach(level => { logger[level] = logFn(level) })
logger.child = () => logger
return logger
}
12 changes: 6 additions & 6 deletions lib/plugin/components/services/statebox/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Statebox = require('@wmfs/statebox')
const stateLint = require('@wmfs/tymly-statelint')
const functionResolver = require('./resources/functions')
const _ = require('lodash')
const { isObject, snakeCase } = require('lodash')

const cls = require('cls-hooked')
const session = cls.createNamespace('statebox')
Expand All @@ -19,13 +19,13 @@ function promiseOrCallback (p, callback) {
class StateboxService {
async boot (options) {
this.services = options.bootedServices

this.logger = options.bootedServices.logger.child('service:statebox')
this.statebox = new Statebox(options)
await this.statebox.ready

addResources(this.statebox, options)

if (!_.isObject(options.blueprintComponents.stateMachines)) {
if (!isObject(options.blueprintComponents.stateMachines)) {
options.messages.detail('No state machines to load :(')
return
}
Expand Down Expand Up @@ -54,7 +54,7 @@ class StateboxService {
const meta = {
name: name,
namespace: definition.namespace,
schemaName: _.snakeCase(definition.namespace),
schemaName: snakeCase(definition.namespace),
comment: definition.comment,
version: definition.version
}
Expand Down Expand Up @@ -183,7 +183,7 @@ class StateboxService {
if (callback) {
return promiseOrCallback(this.sendTaskRevivification(executionName, executionOptions), callback)
}
console.error(`sendTaskRevivification(${executionName})`)
this.logger.error(`sendTaskRevivification(${executionName})`)
return this.reviveIfAuthorised(
executionName,
executionOptions,
Expand Down Expand Up @@ -328,7 +328,7 @@ class StateboxService {
function addResources (statebox, options) {
options.messages.info('Adding resources...')
const stateResources = options.pluginComponents.stateResources
if (!_.isObject(stateResources)) {
if (!isObject(stateResources)) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"Type": "Task",
"Resource": "module:logging",
"ResourceConfig": {
"level": "info",
"template": "Purge site!"
},
"End": true
Expand Down
7 changes: 5 additions & 2 deletions lib/plugin/components/state-resources/logging/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
'use strict'

module.exports = class Logging {
init (resourceConfig) {
init (resourceConfig, env) {
const loggerService = env.bootedServices.logger
this.logger = loggerService.child('stateResource:logging')
this.template = resourceConfig.template
this.level = loggerService.isValidLevel(resourceConfig.level) ? resourceConfig.level : 'info'
}

run (event, context) {
console.log(this.template)
this.logger[this.level](this.template)
context.sendTaskSuccess()
}
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"@hapi/boom": "9.1.2",
"@wmfs/statebox": "1.93.2",
"@wmfs/tymly-statelint": "1.42.0",
"abstract-logging": "2.0.1",
"cls-hooked": "4.2.2",
"debug": "4.3.2",
"dottie": "2.0.2",
Expand Down

0 comments on commit 4026899

Please sign in to comment.