-
-
Notifications
You must be signed in to change notification settings - Fork 34.9k
lib: added logger api in node core #60468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mertcanaltin
wants to merge
9
commits into
nodejs:main
Choose a base branch
from
mertcanaltin:mert/create-logger-api/node-core
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,652
−1
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
16da91e
lib: add logger api in node core
mertcanaltin 81d9f39
lib: address review feedback for logger API
mertcanaltin 4286480
lib: address review feedback for logger API
mertcanaltin 24fefbd
added missing test
mertcanaltin 5f1d732
logger: address review feedback
mertcanaltin f7ef124
logger: address review feedback
mertcanaltin 2478f38
Update lib/internal/logger/serializers.js
mertcanaltin af4a07c
delete pino benchmark
mertcanaltin 7e478d9
delete exports (LEVELS, channels)
mertcanaltin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const fs = require('node:fs'); | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| n: [1e5], | ||
| scenario: [ | ||
| 'string-short', | ||
| 'string-long', | ||
| 'object-simple', | ||
| 'object-nested', | ||
| 'object-array', | ||
| 'object-mixed', | ||
| 'child-logger', | ||
| 'disabled-level', | ||
| 'error-object', | ||
| ], | ||
| }); | ||
|
|
||
| function main({ n, scenario }) { | ||
| const { Logger, JSONConsumer } = require('node:logger'); | ||
|
|
||
| const nullFd = fs.openSync('/dev/null', 'w'); | ||
| const consumer = new JSONConsumer({ stream: nullFd, level: 'info' }); | ||
| consumer.attach(); | ||
|
|
||
| const logger = new Logger({ level: 'info' }); | ||
|
|
||
| switch (scenario) { | ||
| case 'string-short': { | ||
| // Simple short string message | ||
| bench.start(); | ||
| for (let i = 0; i < n; i++) { | ||
| logger.info('hello'); | ||
| } | ||
| bench.end(n); | ||
| break; | ||
| } | ||
|
|
||
| case 'string-long': { | ||
| // Long string message (100 chars) | ||
| const longMsg = 'This is a much longer log message that contains ' + | ||
| 'more text to serialize and process during logging operations'; | ||
| bench.start(); | ||
| for (let i = 0; i < n; i++) { | ||
| logger.info(longMsg); | ||
| } | ||
| bench.end(n); | ||
| break; | ||
| } | ||
|
|
||
| case 'object-simple': { | ||
| // Object with msg and a few string fields | ||
| bench.start(); | ||
| for (let i = 0; i < n; i++) { | ||
| logger.info({ | ||
| msg: 'user action', | ||
| userId: 'user-123', | ||
| action: 'login', | ||
| }); | ||
| } | ||
| bench.end(n); | ||
| break; | ||
| } | ||
|
|
||
| case 'object-nested': { | ||
| // Object with nested structure | ||
| bench.start(); | ||
| for (let i = 0; i < n; i++) { | ||
| logger.info({ | ||
| msg: 'request completed', | ||
| request: { | ||
| method: 'POST', | ||
| path: '/api/users', | ||
| headers: { | ||
| 'content-type': 'application/json', | ||
| 'user-agent': 'Mozilla/5.0', | ||
| }, | ||
| }, | ||
| response: { | ||
| statusCode: 200, | ||
| body: { success: true }, | ||
| }, | ||
| }); | ||
| } | ||
| bench.end(n); | ||
| break; | ||
| } | ||
|
|
||
| case 'object-array': { | ||
| // Object with array fields | ||
| const tags = ['web', 'api', 'auth', 'production']; | ||
| const ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
| bench.start(); | ||
| for (let i = 0; i < n; i++) { | ||
| logger.info({ | ||
| msg: 'batch operation', | ||
| tags, | ||
| processedIds: ids, | ||
| results: ['success', 'success', 'failed', 'success'], | ||
| }); | ||
| } | ||
| bench.end(n); | ||
| break; | ||
| } | ||
|
|
||
| case 'object-mixed': { | ||
| // Mixed types: strings, numbers, booleans, null | ||
| bench.start(); | ||
| for (let i = 0; i < n; i++) { | ||
| logger.info({ | ||
| msg: 'mixed data', | ||
| stringField: 'value', | ||
| numberField: 42, | ||
| floatField: 3.14159, | ||
| booleanField: true, | ||
| nullField: null, | ||
| timestamp: 1704067200000, | ||
| }); | ||
| } | ||
| bench.end(n); | ||
| break; | ||
| } | ||
|
|
||
| case 'child-logger': { | ||
| // Child logger with pre-bound context | ||
| const childLogger = logger.child({ | ||
| service: 'api', | ||
| version: '1.0.0', | ||
| env: 'production', | ||
| }); | ||
| bench.start(); | ||
| for (let i = 0; i < n; i++) { | ||
| childLogger.info({ | ||
| msg: 'request', | ||
| requestId: 'req-123', | ||
| duration: 150, | ||
| }); | ||
| } | ||
| bench.end(n); | ||
| break; | ||
| } | ||
|
|
||
| case 'disabled-level': { | ||
| // Logging at disabled level (debug when level is info) | ||
| bench.start(); | ||
| for (let i = 0; i < n; i++) { | ||
| logger.debug('this will be skipped'); | ||
| } | ||
| bench.end(n); | ||
| break; | ||
| } | ||
|
|
||
| case 'error-object': { | ||
| // Logging with Error object | ||
| const error = new Error('Something went wrong'); | ||
| error.code = 'ERR_SOMETHING'; | ||
| bench.start(); | ||
| for (let i = 0; i < n; i++) { | ||
| logger.error({ | ||
| msg: 'operation failed', | ||
| err: error, | ||
| operation: 'database-query', | ||
| }); | ||
| } | ||
| bench.end(n); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| consumer.flushSync(); | ||
| fs.closeSync(nullFd); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These object benchmarks would also be measuring the construction time of each object constructed within each loop iteration. Not sure if we care about that for this benchmark, but figured it was worth pointing out.