-
Notifications
You must be signed in to change notification settings - Fork 44
feat: shared logger library #3451
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
880992e
feat: init logger lib
dnechay bbf95b6
feat: logger package
dnechay ac96658
fix: logger main entry
dnechay a3014a2
feat: use logger lib in RepO
dnechay 3f60f42
chore: do not use incremental build in logger TS
dnechay 03139be
feat: add logger cd
dnechay 1007b30
chore: add permissions to logger cd action
dnechay 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
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,26 @@ | ||
| name: Logger NPM publish | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
|
|
||
| jobs: | ||
| logger-publish: | ||
| name: Logger NPM Publish | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/setup-node@v4 | ||
| with: | ||
| node-version-file: .nvmrc | ||
| cache: yarn | ||
| - name: Install dependencies | ||
| run: yarn install --immutable | ||
| - name: Build logger package | ||
| run: yarn build:logger | ||
| - name: Publish package | ||
| run: yarn workspace @human-protocol/logger npm publish --access public | ||
| env: | ||
| YARN_NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
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
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
1 change: 1 addition & 0 deletions
1
packages/apps/reputation-oracle/server/src/common/constants/index.ts
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
86 changes: 0 additions & 86 deletions
86
packages/apps/reputation-oracle/server/src/logger/README.md
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
packages/apps/reputation-oracle/server/src/logger/__mocks__/index.ts
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
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,4 @@ | ||
| { | ||
| "singleQuote": true, | ||
| "trailingComma": "all" | ||
| } |
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,152 @@ | ||
| ## HUMAN Protocol Node.js logger | ||
|
|
||
| Custom logger implementation for use in Node.js HUMAN Protocol apps. | ||
|
|
||
| Provides standartized API and writes logs in pre-defined JSON format: | ||
| ```json | ||
| { | ||
| "level": "debug", | ||
| "message": "Some info about error", | ||
| "timestamp": 1738937400037, | ||
| "error": { | ||
| "kind": "CustomError", | ||
| "message": "Message from custom error", | ||
| "stack": "CustomError: Message from custom error\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)", | ||
| "customErrorProp": "error-detail-prop" | ||
| } | ||
| // ...merging meta | ||
| } | ||
| ``` | ||
|
|
||
| ## Logger initialization and usage | ||
|
|
||
| ### Pre-created logger instance | ||
| By defaul this library exports pre-created logger instance that you can start using immediately. This logger is pre-configured with: | ||
| - zero-bindings (only `name` is set up as `human-protocol-logger`) | ||
| - pretty-printing and structured log format | ||
| - has `debug` as its minimum level to log | ||
|
|
||
| To use it: | ||
| ```ts | ||
| import logger from '@human-protocol/logger'; | ||
|
|
||
| logger.info('Some message', { some: 'extras' }); | ||
| ``` | ||
| It's expected to be used only in dev environments, such as a developer toolbox, some local scripts and etc. | ||
|
|
||
| ### Create application logger with factory function | ||
|
|
||
| Lib exports `createLogger` factory function that should be used to create a logger intance with proper bindings for application. Recommended bindings are at least `service` and `environment`, so that you can distinguish logs coming from different sources in your log-management system easilly. | ||
|
|
||
| ```ts | ||
| import { createLogger, LogLevel } from '@human-protocol/logger'; | ||
|
|
||
| const ENV_NAME = process.NODE_ENV || 'development'; | ||
| const isDevelopment = ENV_NAME === 'development'; | ||
| const isTest = process.NODE_ENV === 'test' | ||
|
|
||
| const defaultAppLogger = createLogger( | ||
| { | ||
| name: 'DefaultLogger', | ||
| level: isDevelopment ? LogLevel.DEBUG : LogLevel.INFO, | ||
| pretty: isDevelopment, | ||
| disabled: isTest, | ||
| }, | ||
| { | ||
| environment: ENV_NAME, | ||
| service: 'you-application-name', | ||
| }, | ||
| ); | ||
|
|
||
| export default defaultAppLogger; | ||
|
|
||
| // import it somewehre in your app and use | ||
| import logger from '<relative_path_to>/logger'; | ||
|
|
||
| logger.info('Some application log', { with: 'useful-context' }); | ||
| ``` | ||
|
|
||
| ### Replacing default Nest.js logger | ||
| Once you have your application logger instance created, you can replace default Nest.js logger with it, so anything that comes from your appication follows the same format. This library provides a helper for that: | ||
|
|
||
| ```ts | ||
| import { NestLogger } from '@human-protocol/logger'; | ||
|
|
||
| // initialize an overridden instance | ||
| export const nestLoggerOverride = new NestLogger( | ||
| defaultAppLogger.child({ name: 'NestLogger' }), | ||
| ); | ||
|
|
||
|
|
||
| // init you Nest.js app with overriden instance | ||
| const app = await NestFactory.create(AppModule, { | ||
| logger: nestLoggerOverride, | ||
| }); | ||
|
|
||
| // Logger is `nestLoggerOverride` now | ||
| import { Logger } from '@nestjs/common'; | ||
| ``` | ||
|
|
||
| ## Usage examples | ||
| Logger has next API contract: log message first and any useful detail in log meta as a second argument. | ||
|
|
||
| :information_source: If meta is not a plain object and not an error - it is being logged as "meta" property | ||
|
|
||
| :information_source: Error formatting is done only for error argument and "error" context property. | ||
|
|
||
| ```ts | ||
| // to log simple message | ||
| logger.info('Info without any context'); | ||
|
|
||
| // to log message with some context info | ||
| loger.info('New user registered', { | ||
| userAgent, | ||
| country, | ||
| ip, | ||
| email, | ||
| }); | ||
|
|
||
| // to log an error | ||
| logger.error('Something went wrong', error); | ||
|
|
||
| // or error with some extra context | ||
| logger.error('Error updating some entity', { | ||
| error, | ||
| entityId: 42, | ||
| ...extra | ||
| }); | ||
|
|
||
| // to log anything | ||
| logger.debug('Show me value', someValue); | ||
| ``` | ||
|
|
||
| If needed, you can create a named logger | ||
| ```ts | ||
| const utilsLogger = logger.child({ name: 'UtilsLogger' }) | ||
| // .... | ||
| utilsLogger.debug('input params', args); | ||
| /* | ||
| { | ||
| "level": "debug", | ||
| "logger": "UtilsLogger", | ||
| "message": "input params", | ||
| "timestamp": 1738937400037 | ||
| ... | ||
| } | ||
| */ | ||
| ``` | ||
| or bind a "context" (or any other) prop to it | ||
| ```ts | ||
| const contextLogger = logger.child({ context: 'SomeClassOrMethod' }) | ||
| // .... | ||
| contextLogger.info('Helpful info', context); | ||
| /* | ||
| { | ||
| "level": "debug", | ||
| "context": "SomeClassOrMethod", | ||
| "message": "Helpful info", | ||
| "timestamp": 1738937400037, | ||
| ... | ||
| } | ||
| */ | ||
| ``` |
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.
Uh oh!
There was an error while loading. Please reload this page.