winston-error-format
Version:
Log errors with Winston
44 lines (30 loc) • 987 B
JavaScript
import isErrorInstance from"is-error-instance";
import{format}from"logform";
import{LEVEL,MESSAGE}from"triple-beam";
import{toFullLogObject}from"./full.js";
import{DEFAULT_LEVEL,validateOptions}from"./options.js";
import{toShortLogObject}from"./short.js";
export{validateOptions};
const getFormat=(method,options)=>{
if(options!==undefined&&typeof options!=="function"){
validateOptions(options)
}
return format(formatFunc.bind(undefined,method,options))()
};
const formatFunc=(method,options,value)=>{
if(!isErrorInstance(value)){
return value
}
const{level=DEFAULT_LEVEL}=value;
deleteWinstonProps(value);
const object=method(value,level,options);
return{...object,[LEVEL]:object.level}
};
const deleteWinstonProps=(value)=>{
WINSTON_PROPS.forEach((propName)=>{
delete value[propName]
})
};
const WINSTON_PROPS=["level",LEVEL,MESSAGE];
export const shortFormat=getFormat.bind(undefined,toShortLogObject);
export const fullFormat=getFormat.bind(undefined,toFullLogObject);