winston
๋ฒ์ ? _winston@2
winston@3
node -v
์ถ๋ ฅ : _ v9.8.0logger.stream ๊ฐ์ฒด์ ์ฐ๊ธฐ ํจ์๋ฅผ ๋ง๋ค๋ ค๊ณ ํ ๋ typescript ์ค๋ฅ๊ฐ ์์ต๋๋ค.
error TS2322: Type '{ write(message: any, encoding: any): void; }' is not assignable to type '(options?: any) => ReadableStream'.
Object literal may only specify known properties, and 'write' does not exist in type '(options?: any) => ReadableStream'.
39 write(message: any, encoding: any) {
~~~~~
์ค๋ฅ ์์
const options = {
file: {
level: "info",
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
},
console: {
level: "debug",
handleExceptions: true,
json: false,
colorize: true,
},
};
this.logger = winston.createLogger({
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console),
],
exitOnError: false, // do not exit on handled exceptions
});
// create a stream object with a 'write' function that will be used by `morgan`
this.logger.stream = {
write(message: any, encoding: any) {
// use the 'info' log level so the output will be picked up by both transports (file and console)
this.logger.info(message);
},
};
์ด ํํ ๋ฆฌ์ผ์ ๋ฐ๋ฅด๊ณ ์์ต๋๋ค.
https://www.digitalocean.com/community/tutorials/how-to-use-winston-to-log-node-js-applications
๊ฐ์ ์ค๋ฅ๊ฐ์๋ค
์ธ๋ป๋ณด๊ธฐ์ ๋ฌธ์ ๊ฐ ์ ํ ์ ์์์๋ ๊ฒ์ฒ๋ผ ๋ณด์
๋๋ค. stream(options?: any): NodeJS.ReadableStream;
์ด์ค์ด์ด์ผํฉ๋๋ค.
ํธ์ง : ์ค์ ๋ก ์ธ ์์์ ์๋ ์์ต๋๋ค. ์๋์ํฌ ์ ์๋์ง ๋ณผ๊ฒ์.
@DABH ์ด๊ฒ ์ข ๋ณผ ์ ์์ต๋๊น?
stream
๋ฅผ ๋ก๊ฑฐ์ ์ง์ ์ถ๊ฐํ๊ธฐ ๋๋ฌธ์ ๋ฌธ์ ๊ฐ๋ฉ๋๋ค. ๋์ ๋ค์์ ์๋ํด๋ณด์ธ์.
// At the bottom of your winston config add
export const stream = {
write: (message) => {
logger.info(message);
},
};
๊ทธ๋ฐ ๋ค์ ๋ค์๊ณผ ๊ฐ์ด ๋ก๊ทธ์ธ ์ต์คํ๋ ์ค๋ฅผ ๋ณ๊ฒฝํ์ญ์์ค.
import { stream } from 'config/winston.ts';
import * as morgan from 'morgan';
app.use(morgan('combined', { stream }));
๊ฒ์ ํ ์ค๋ฅ๋ก ์ธํด typescript๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค๊ณ ๊ฐ์ ํ๋ฏ๋ก ๋ด ์์ ๋ typescript๋ก ์์ฑ๋์์ต๋๋ค. ํ๋์ ์์ ๋ฌธ์ , ๋ค์๊ณผ ๊ฐ์ด ์คํธ๋ฆผ์ ์์ฑํ๊ณ ์ถ์ ์ ์์ต๋๋ค.
export const stream = {
write: logger.info,
};
๊ทธ๋ฌ๋ ์คํธ๋ฆผ ํจ์์ ๋ ๋ฒ์งธ ์ธ์๊ฐ ๋ก๊ฑฐ info
ํจ์๊ฐ ์์ํ๋ ๊ฒ๊ณผ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ์์ธ๊ฐ ๋ฐ์ํฉ๋๋ค. ๋์์ด๋๋์ง ์๋ ค์ฃผ์ธ์.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
stream
๋ฅผ ๋ก๊ฑฐ์ ์ง์ ์ถ๊ฐํ๊ธฐ ๋๋ฌธ์ ๋ฌธ์ ๊ฐ๋ฉ๋๋ค. ๋์ ๋ค์์ ์๋ํด๋ณด์ธ์.๊ทธ๋ฐ ๋ค์ ๋ค์๊ณผ ๊ฐ์ด ๋ก๊ทธ์ธ ์ต์คํ๋ ์ค๋ฅผ ๋ณ๊ฒฝํ์ญ์์ค.
๊ฒ์ ํ ์ค๋ฅ๋ก ์ธํด typescript๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค๊ณ ๊ฐ์ ํ๋ฏ๋ก ๋ด ์์ ๋ typescript๋ก ์์ฑ๋์์ต๋๋ค. ํ๋์ ์์ ๋ฌธ์ , ๋ค์๊ณผ ๊ฐ์ด ์คํธ๋ฆผ์ ์์ฑํ๊ณ ์ถ์ ์ ์์ต๋๋ค.
๊ทธ๋ฌ๋ ์คํธ๋ฆผ ํจ์์ ๋ ๋ฒ์งธ ์ธ์๊ฐ ๋ก๊ฑฐ
info
ํจ์๊ฐ ์์ํ๋ ๊ฒ๊ณผ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ์์ธ๊ฐ ๋ฐ์ํฉ๋๋ค. ๋์์ด๋๋์ง ์๋ ค์ฃผ์ธ์.