@c2m/c2m-logger
v1.0.20
Published
1. npm i 2. npm audit 3. npm run build-wim/build-linux 4. cd dist 1. if needed login: npm login --auth-type=web 5. npm publish --otp=[opt number here]
Downloads
128
Readme
C2M Logger
how to deploy:
- npm i
- npm audit
- npm run build-wim/build-linux
- cd dist
- if needed login: npm login --auth-type=web
- npm publish --otp=[opt number here]
General usage
You can unset the logger's formatting when printing to the console by setting an environment variable named NO_LOG_FORMAT to anything truthy (true,non-empty string etc.)
- Initial setup example (in main.ts):
import { logger } from "@c2m/c2m-logger";
/*
This is the class we're using for the logger's options.
For most projects, just specifiying the PROJECT_NAME (e.g. PROJECT_NAME: "PCLR")
and have LOG_LEVEL: process.env.LOG_LEVEL will suffice.
export class LoggerOptions{
PROJECT_NAME?: string; Name of the project
LOG_NAME?: string; Name of the log file (has default according to PROJECT_NAME)
LOG_DIRNAME?: string; Directory path for log file
LOG_LEVEL?: string; Level from which we're logging into the file
LOG_KEEP_FILES?: string; Max number of files we'd like to keep on disk (e.g. 10 will mean we'll rotate out the oldest log file once we hit 10 files)
LOG_FREQUENCY?: string; How often we want to create a new file (see https://github.com/winstonjs/winston-daily-rotate-file)
}
*/
logger.setconfig({
PROJECT_NAME: <name of the project>,
LOG_LEVEL: process.env.LOG_LEVEL
}
- Usage example:
import { logger } from "@c2m/c2m-logger";
//Passing objects to print out
let item = {name: "honey", price: 50, weight: 0.35}
logger.info("user wanted to buy item", item);
//Multiple objects
let posResponse = buyItem(item);
logger.info("response from pos for buying item", {posResponse, item});
//logging errors
catch(exception){
//Will automatically extract the error message from the error object
logger.error(exception);
// Not yet madnatory but about to be in the future:
// we want to have an array of errors in each process to streamline
// the different kind of errors that we see. For example:
// import {C2MError} from '@c2m/c2m-logger'
// errors: Object = {
// POS_FAIL_BUY: {code: 50, description: "POS failed on buy item"}
// }
//
//Then when logging an error you should pass the appropriate C2MError object to the logger:
//POS returned fail on buying item:
posRes = buyItem(item);
catch(exception){
logger.error(exception, {posRes, item}, errors.POS_FAIL_BUY);
}
}
ELK setup
ElkLoggerOptions
should get the following properties:
ELK_HOST
- the host of the ELK serverELK_AUTH
- the authorization token of the ELK serverLOG_LEVEL
(optional) - the log level of the logger. default iswarn
(only logs from level 'warn' and above will be sent to the ELK)
in order to send logs to ELK, you need to configure the logger with the following ElkLoggerOptions
:
Usage
import { logger, ElkLoggerOptions } from "@c2m/c2m-logger";
const elkOptions: ElkLoggerOptions = {
ELK_HOST: <host url from cart settings>,
ELK_AUTH: <token from cart settings>
};
logger.setELKConfig(elkOptions);
//setup of process metadata so we can filter the logs by
//which process sent this. for example:
const metadata: IElkCartMetadata = {
processName: "PCLR",
cartId: this.getConfig('cartID') as string,
branchId: this.getConfig('branchId') as string
};
logger.setProcessMetadata(metadata);
//Extend the IElkMetadata interface to include whatever metadata you think is relevant
//to the process (for the PCLR, include the cartID and branchId, for the device manager maybe include arduino version etc.)