Skip to main content

Logging

Logger is part of the Context. This allows you to use the logger from within any flow that includes the context such as a filter or transformer.

There are 4 log levels:

  • debug should be used for development, testing, and debugging purposes only
  • info should be used for your information only
  • warn should be used for warnings that should be fixed, but are not show stoppers
  • error should be used for show-stopping errors

Log Levels are configurable per each channel. This allows some channels to log at a higher level while others are active in production and logging with a lower level. In the OOP style, use the logLevel method.

Setting the Log Level logs everything up to that level. For instance, using the warn log level will log both error and warn events.

The logger is fairly simple to use as seen in this example of a filter step:

logExample.ts
import gofer from '@gofer-engine/engine';

gofer
.listen('tcp', 'localhost', 5575)
.logLevel('debug')
.filter((msg, context) => {
const isFemale = msg.get('PID-8')==='F';
const { messageId, logger } = context;
if (!isFemale) {
logger(`Msg ${messageId} was not a Female`, 'info');
}
return isFemale;
})
.run();

The logger is by default set up to log with console.log. You can replace the default logger using the global "LOGGER" from Gofer Engine's Event System, handelse.

logExample.ts
import gofer from '@gofer-engine/engine';
import handelse from '@gofer-engine/handelse';
import fs from 'fs';

const logStream = fs.createWriteStream('/tmp/gofer.log', { flags: 'a', autoClose: true });

const LOGGER = handelse.get<string>('LOGGER');

LOGGER.removeAll();

LOGGER.sub((log) => {
logStream.write(log + '\n');
return true;
});

gofer
.listen('tcp', 'localhost', 5575)
.logLevel('debug')
.filter((msg, context) => {
const isFemale = msg.get('PID-8')==='F';
const { messageId, logger } = context;
if (!isFemale) {
logger(`Msg ${messageId} was not a Female`, 'info');
}
return isFemale;
})
.run();