์ฌ๋ฌ JSON ๊ตฌ์กฐ ์ prettyPrint ์ต์ ์ ์ฌ์ฉํ์ฌ ๊ฐ๋ ฅํ ๋ก๊น ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ winston์ CPU๋ฅผ 90% ์ด์์ผ๋ก ์คํ์ดํฌํฉ๋๋ค.
๋ค์๊ณผ ๊ฐ์ด ๊ตฌ์ฑ๋ ์ ์ก์ ์ฌ์ฉํ์ฌ ๋ฌธ์ ๋ฅผ ์ฌํํฉ๋๋ค.
new (winston.transports.Console)({
level: 'silly',
silent: false,
colorize: true,
prettyPrint: true,
timestamp: true,
humanReadableUnhandledException: true,
format: format.combine(
format.splat(),
format.colorize(),
format.label({ label: 'Foo' }),
format.timestamp(),
format.prettyPrint(),
format.printf( log => {
return '[ ' + log.timestamp + ' ][ ' + log.level + ' ][ ' + log.label + ' ] ' + log.message;
})
)
})
https://github.com/winstonjs/winston/issues/613 ๊ณผ ๊ด๋ จ์ด ์์ ์ ์์ต๋๋ค.
Winston ๋ฐ prettyPrint๋ฅผ ์ฌ์ฉํ์ฌ CPU ์คํ์ดํฌ ์์ ํ์ฑํ
PrettyPrint๋ฅผ ๋นํ์ฑํํ๊ฑฐ๋ ๋ก๊น ์ ์ JSON ๊ตฌ์กฐ์ ๋ฌธ์์ดํ๋ ๋ฒ์ ์ ์ฌ์ฉํ๋ฉด CPU ์คํ์ดํฌ๊ฐ ์์ ํ ์ฌ๋ผ์ง๋๋ค.
CPU ์คํ์ดํฌ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์์ ๋ ์ ์ก ๊ตฌ์ฑ:
new (winston.transports.Console)({
level: 'silly',
silent: false,
colorize: true,
prettyPrint: false, // Forced disabled pretty print
timestamp: true,
humanReadableUnhandledException: true,
format: format.combine(
format.splat(),
format.colorize(),
format.label({ label: 'Foo' }),
format.timestamp(),
//format.prettyPrint(), // Removed prettyPrint from format
format.printf( log => {
return '[ ' + log.timestamp + ' ][ ' + log.level + ' ][ ' + log.label + ' ] ' + log.message;
})
)
})
JSON ๊ฐ์ฒด๊ฐ ์ผ๋ง๋ ๊น๊ณ ๋ณต์กํฉ๋๊น? ์ฐ๋ฆฌ๊ฐ ์์ ์ธ์์ ์ฌ์ฉํ๋ util.inspect๋ ์ถฉ๋ถํ ๋ณต์กํ ๊ฐ์ฒด์ ๋ํด ๋งค์ฐ ๋๋ ค์ง ์ ์์ต๋๋ค(์ํ ๊ตฌ์กฐ๊ฐ ์๋ ๊ฒฝ์ฐ ๋ฌดํ ๋ฃจํ์ผ ์๋ ์์). ์ ์ฒด ๊ฐ์ฒด๋ฅผ ๊ธฐ๋กํด์ผ ํฉ๋๊น, ์๋๋ฉด ๊ฐ์ฒด์ ์ฒ์ ๋ช ๋ ์ด์ด(๊น์ด ๋งค๊ฐ๋ณ์)๋ง์ผ๋ก ๋ง์กฑํฉ๋๊น?
์ง๊ธ ๋ด๊ฐ ๊ธฐ๋กํ๊ณ ์๋ JSON ๊ตฌ์กฐ๋ ๋ณธ์ง์ ์ผ๋ก ๊ฐ์ฒด์ ๋ฐฐ์ด์ ๋๋ค. ๊ฐ์ฒด๋ ์ธ์คํด์คํ๋ ํด๋์ค์ด๊ณ ๊ทธ ์๋ 10~100๊ฐ ํญ๋ชฉ์ด๋ฉฐ ๋ณต์ก์ฑ์ ๊น์ด ์์ค 3(์ํ ๊ตฌ์กฐ ์์)์ ๋๋ฌํ์ง ์์ต๋๋ค.
๊ทธ๋ฌ๋ ๋๋ ๊ทธ๊ฒ๋ค์ ๋งค์ฐ ์์ฃผ ๊ธฐ๋กํฉ๋๋ค: 200/500ms๋ง๋ค ํ๋์ ๋ก๊ทธ์ ์ต๋ ๋น๋์ ๋๋ฌํฉ๋๋ค.
๋ณต์กํ ๊ตฌ์กฐ์ ๋ก๊น
์ "์ด๋ฆฌ์์" ์ฅํฉํจ์ ์ฌ์ฉํ ๋ ๊ฐ๋ฐ ํ๊ฒฝ์์๋ง ์ ์ฉํฉ๋๋ค.
"์ด๋ฆฌ์์" ์์ธํ ์ ๋ณด๋ "๋ฐ๋ณด ๊ฐ์" ์์ค์ด์ด์ผ ํ๊ธฐ ๋๋ฌธ์(^^) ์ ๋ ๊ทธ๋ฅ ์์ ์ธ์๋ฅผ ๋นํ์ฑํํ๊ณ ๊น์ด ์ ํ, ์ง์ฐ ๋๋ ๊ณผ๋ํ CPU ์ฌ์ฉ๋ ์์ด ๋ณต์กํ ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ ์ป์์ผ๋ก์จ ํด๊ฒฐํ์ต๋๋ค.
"util.inspect"๊ฐ JSON.stringify์ ๋นํด ๊ทธ๋ ๊ฒ ๋๋ฆด ์ ์๋์ง ๋ชฐ๋์ต๋๋ค. ์๋ง๋ ๊น์ด ๋งค๊ฐ๋ณ์๋ ์คํ์ดํฌ๋ฅผ ์ ๊ฑฐํ๊ธฐ์ ์ถฉ๋ถํ ์ ์์ต๋๋ค(์ํฉ์ ๋ ์ ์ดํดํ๊ธฐ ์ํด ๋ ๋ง์ ๋ฐ์ดํฐ๊ฐ ํ์ํ ๊ฒฝ์ฐ ์ฌ๊ฐ ์๊ฐ์ ํ ์คํธํ ์ ์์)
winston ๋๋ logform์ ๋ช ๊ฐ์ง ๋ฌธ์๋ฅผ ์ถ๊ฐํด์ผ ํ๊ธฐ ๋๋ฌธ์ ์ด๊ฒ์ ์ด์ด ๋ก๋๋ค.
util.inspect์ Node.js ๋ฌธ์์์(https://nodejs.org/api/util.html#util_util_inspect_object_options):
util.inspect()๋ ์ฃผ๋ก ๋๋ฒ๊น ๋๊ตฌ๋ก ์ฌ์ฉ๋๋ ๋๊ธฐ ๋ฉ์๋์ ๋๋ค. ์ผ๋ถ ์ ๋ ฅ ๊ฐ์๋ ์ด๋ฒคํธ ๋ฃจํ๋ฅผ ์ฐจ๋จํ ์ ์๋ ์๋นํ ์ฑ๋ฅ ์ค๋ฒํค๋๊ฐ ์์ ์ ์์ต๋๋ค. ์ด ํจ์๋ ์ฃผ์ํด์ ์ฌ์ฉํ๊ณ ํซ ์ฝ๋ ๊ฒฝ๋ก์์๋ ์ฌ์ฉํ์ง ๋ง์ญ์์ค.
๋ฐ๋ผ์ Node๋ ํ๋ก๋์ ์ฝ๋์์ ์ด๋ฅผ ์ฌ์ฉํ์ง ์์ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค. ๋ฐ๋ผ์ ์ฌ์ฉ์๊ฐ ํ๋ก๋์ ์ฝ๋์์ prettyPrint๋ฅผ ์ฌ์ฉํด์๋ ์ ๋๋ค๊ณ ์ธ๊ธํด์ผ ํฉ๋๋ค. ์ฆ, prettyPrint์ ๋ํ ๊น์ด ๋งค๊ฐ๋ณ์๋ฅผ ๋ ธ์ถํ๋ฉด ์ฑ๋ฅ์ ๋์์ด ๋ ์ ์์ต๋๋ค.
์ฌ๋ ค์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ์ค์ํ ๋ฉ๋ชจ๋ผ๊ณ ์๊ฐํฉ๋๋ค!
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
winston ๋๋ logform์ ๋ช ๊ฐ์ง ๋ฌธ์๋ฅผ ์ถ๊ฐํด์ผ ํ๊ธฐ ๋๋ฌธ์ ์ด๊ฒ์ ์ด์ด ๋ก๋๋ค.
util.inspect์ Node.js ๋ฌธ์์์(https://nodejs.org/api/util.html#util_util_inspect_object_options):
๋ฐ๋ผ์ Node๋ ํ๋ก๋์ ์ฝ๋์์ ์ด๋ฅผ ์ฌ์ฉํ์ง ์์ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค. ๋ฐ๋ผ์ ์ฌ์ฉ์๊ฐ ํ๋ก๋์ ์ฝ๋์์ prettyPrint๋ฅผ ์ฌ์ฉํด์๋ ์ ๋๋ค๊ณ ์ธ๊ธํด์ผ ํฉ๋๋ค. ์ฆ, prettyPrint์ ๋ํ ๊น์ด ๋งค๊ฐ๋ณ์๋ฅผ ๋ ธ์ถํ๋ฉด ์ฑ๋ฅ์ ๋์์ด ๋ ์ ์์ต๋๋ค.
์ฌ๋ ค์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ์ค์ํ ๋ฉ๋ชจ๋ผ๊ณ ์๊ฐํฉ๋๋ค!