log.ts
import log4js from "log4js";
import path from "path";
import util from "util";
const logDirectory = path.join(process.cwd(), "logs");
const logLayout = {
type: "pattern",
pattern: "%d %p %c %f:%l %x{singleLine}",
tokens: {
singleLine: function (logEvent: { data: Array<unknown> }) {
return logEvent.data
.map((d) => {
if (
typeof d === "boolean" ||
typeof d === "number" ||
typeof d === "string"
) {
return d.toString().replace(/\n/gm, "\\n");
} else {
return util
.inspect(d, { breakLength: Infinity })
.replace(/\n/gm, "\\n");
}
})
.filter((d) => d.length > 0)
.join(" ");
},
},
};
log4js.configure({
appenders: {
console: {
type: "console",
layout: logLayout,
},
app: {
type: "dateFile",
layout: logLayout,
filename: path.join(logDirectory, "app.log"),
pattern: "-yyyy-MM-dd",
daysToKeep: 7,
compress: true,
},
},
categories: {
default: {
appenders: ["console", "app"],
level: "all",
enableCallStack: true,
},
},
});
import { getLogger } from "log4js";
const logger = getLogger();
// 略
logger.info("enqueued:", sqsResponse);
logger.error(new Error("piyo"));
export interface ILogging {
Logging(str:string):any
}
export class LoggingClass implements ILogging {
Logging(logstr:string):any {
const log4js = require('log4js');
log4js.configure({
appenders : {
app : {type : 'file', filename : './log/app.log'}
},
categories : {
default : {appenders : ['app'], level: 'debug'},
}
});
const logger = log4js.getLogger('app');
logger.debug(logstr)
}
}
configure({
appenders: {
console: {
type: "console"
},
logfile: { type: "file", filename: "log/app.log" }
},
categories: { default: { appenders: ["console", "logfile"], level: "error" } }
});