Genesys Cloud Client Logger
Logger to send client logs to a remote server.
See CHANGELOG.md for version updates.
Install
npm install genesys-cloud-client-logger
Basic Concept
Each Logger instance will have it's own configuration meaning you can have multiple apps using their own individual loggers. One thing to note is the loggers
will share a "log-uploader" for each given url
. For example, if app1
and app2
both POST logs to the same endpoint, they will have their own logger and
config, but will share the same uploader. Meaning only one POST request will happen at a time. This is to help reduce rate limiting by having multiple loggers
all sending POST requests to the same endpoint at the same time.
Usage
import { Logger } from 'genesys-cloud-client-logger';
const logger = new Logger({
url: 'https://yoursite.com/logs',
accessToken: 'your-access-token',
appVersion: '1.2.3',
appName: 'your-client-app1'
});
logger.info('Logger initialized');
Available options and their defaults:
interface ILoggerConfig {
accessToken: string;
url: string;
appVersion: string;
appName: string;
originAppName?: string;
originAppVersion?: string;
originAppId?: string;
initializeServerLogging?: boolean;
logLevel?: LogLevel;
uploadDebounceTime?: number;
debugMode?: boolean;
stringify?: boolean;
logger?: ILogger;
formatters?: LogFormatterFn[]
}
Logging messages
log (message: string | Error, details?: any, opts?: ILogMessageOptions): void;
interface ILogMessageOptions {
skipDefaultFormatter?: boolean,
skipServer?: boolean,
skipSecondaryLogger?: boolean,
}
The default formatter handles extracting the message from an error object as well as prepending the app name to the
message that will be logged. For example, if your app name is "really cool app" and you do something like this:
logger.info('some message I care about', { favoriteColor: 'blue' });
It will be logged like this:
[really cool app] some message I care about {...}
If you were to log a message like this:
logger.info('some message I care about', { favoriteColor: 'blue' }, { skipDefaultFormatter: true });
It would be logged without the app name:
some message I care about {...}
How Formatters Work
Formatters are a great tool to handle unique logging situations. For example, let's say
you have an error that has the potential to expose or send information that is unfit to
be exposed. In a formatter, you can choose to manipulate the message or details, do
nothing, or skip logging the message entirely. A formatter will be provided a next
function in addition to the log message. If next is not called, the log will not be forwarded
to downstream formatters and will not make it to the actual logger. Example:
function myCustomFormatter (
level: LogLevel,
message: string | Error,
details: any | undefined,
options: ILogMessageOptions,
next: NextFn
) {
if (message.includes('[confidential]')) {
options.skipServer = true;
return next(level, 'this message is confidential and redacted', details, options);
}
if (message.includes('[top secret]')) {
return;
}
next();
}
const logger = new Logger({
url: 'https://yoursite.com/logs',
accessToken: 'your-access-token',
appVersion: '1.2.3',
appName: 'your-client-app1',
formatters: [ myCustomFormatter ]
});
logger.info('here is a message');
logger.info('here is a [confidential] message');
logger.info('here is a [top secret] message');