feat(ci): Add GitHub Actions workflows for CI/CD
This commit is contained in:
parent
af18c2f57a
commit
d16e3b613c
66
.gitea/workflows/default_nottags.yaml
Normal file
66
.gitea/workflows/default_nottags.yaml
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
name: Default (not tags)
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags-ignore:
|
||||||
|
- '**'
|
||||||
|
|
||||||
|
env:
|
||||||
|
IMAGE: code.foss.global/host.today/ht-docker-node:npmci
|
||||||
|
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@/${{gitea.repository}}.git
|
||||||
|
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
|
||||||
|
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
|
||||||
|
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
|
||||||
|
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
security:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
continue-on-error: true
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Install pnpm and npmci
|
||||||
|
run: |
|
||||||
|
pnpm install -g pnpm
|
||||||
|
pnpm install -g @ship.zone/npmci
|
||||||
|
|
||||||
|
- name: Run npm prepare
|
||||||
|
run: npmci npm prepare
|
||||||
|
|
||||||
|
- name: Audit production dependencies
|
||||||
|
run: |
|
||||||
|
npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
npmci command pnpm audit --audit-level=high --prod
|
||||||
|
continue-on-error: true
|
||||||
|
|
||||||
|
- name: Audit development dependencies
|
||||||
|
run: |
|
||||||
|
npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
npmci command pnpm audit --audit-level=high --dev
|
||||||
|
continue-on-error: true
|
||||||
|
|
||||||
|
test:
|
||||||
|
if: ${{ always() }}
|
||||||
|
needs: security
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Test stable
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm install
|
||||||
|
npmci npm test
|
||||||
|
|
||||||
|
- name: Test build
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm install
|
||||||
|
npmci npm build
|
124
.gitea/workflows/default_tags.yaml
Normal file
124
.gitea/workflows/default_tags.yaml
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
name: Default (tags)
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- '*'
|
||||||
|
|
||||||
|
env:
|
||||||
|
IMAGE: code.foss.global/host.today/ht-docker-node:npmci
|
||||||
|
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@/${{gitea.repository}}.git
|
||||||
|
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
|
||||||
|
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
|
||||||
|
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
|
||||||
|
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
security:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
continue-on-error: true
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
run: |
|
||||||
|
pnpm install -g pnpm
|
||||||
|
pnpm install -g @ship.zone/npmci
|
||||||
|
npmci npm prepare
|
||||||
|
|
||||||
|
- name: Audit production dependencies
|
||||||
|
run: |
|
||||||
|
npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
npmci command pnpm audit --audit-level=high --prod
|
||||||
|
continue-on-error: true
|
||||||
|
|
||||||
|
- name: Audit development dependencies
|
||||||
|
run: |
|
||||||
|
npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
npmci command pnpm audit --audit-level=high --dev
|
||||||
|
continue-on-error: true
|
||||||
|
|
||||||
|
test:
|
||||||
|
if: ${{ always() }}
|
||||||
|
needs: security
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
run: |
|
||||||
|
pnpm install -g pnpm
|
||||||
|
pnpm install -g @ship.zone/npmci
|
||||||
|
npmci npm prepare
|
||||||
|
|
||||||
|
- name: Test stable
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm install
|
||||||
|
npmci npm test
|
||||||
|
|
||||||
|
- name: Test build
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm install
|
||||||
|
npmci npm build
|
||||||
|
|
||||||
|
release:
|
||||||
|
needs: test
|
||||||
|
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
run: |
|
||||||
|
pnpm install -g pnpm
|
||||||
|
pnpm install -g @ship.zone/npmci
|
||||||
|
npmci npm prepare
|
||||||
|
|
||||||
|
- name: Release
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm publish
|
||||||
|
|
||||||
|
metadata:
|
||||||
|
needs: test
|
||||||
|
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
continue-on-error: true
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
run: |
|
||||||
|
pnpm install -g pnpm
|
||||||
|
pnpm install -g @ship.zone/npmci
|
||||||
|
npmci npm prepare
|
||||||
|
|
||||||
|
- name: Code quality
|
||||||
|
run: |
|
||||||
|
npmci command npm install -g typescript
|
||||||
|
npmci npm install
|
||||||
|
|
||||||
|
- name: Trigger
|
||||||
|
run: npmci trigger
|
||||||
|
|
||||||
|
- name: Build docs and upload artifacts
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm install
|
||||||
|
pnpm install -g @git.zone/tsdoc
|
||||||
|
npmci command tsdoc
|
||||||
|
continue-on-error: true
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -3,7 +3,6 @@
|
|||||||
# artifacts
|
# artifacts
|
||||||
coverage/
|
coverage/
|
||||||
public/
|
public/
|
||||||
pages/
|
|
||||||
|
|
||||||
# installs
|
# installs
|
||||||
node_modules/
|
node_modules/
|
||||||
@ -17,4 +16,4 @@ node_modules/
|
|||||||
dist/
|
dist/
|
||||||
dist_*/
|
dist_*/
|
||||||
|
|
||||||
# custom
|
#------# custom
|
128
.gitlab-ci.yml
128
.gitlab-ci.yml
@ -1,128 +0,0 @@
|
|||||||
# gitzone ci_default
|
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
|
||||||
|
|
||||||
cache:
|
|
||||||
paths:
|
|
||||||
- .npmci_cache/
|
|
||||||
key: '$CI_BUILD_STAGE'
|
|
||||||
|
|
||||||
stages:
|
|
||||||
- security
|
|
||||||
- test
|
|
||||||
- release
|
|
||||||
- metadata
|
|
||||||
|
|
||||||
before_script:
|
|
||||||
- pnpm install -g pnpm
|
|
||||||
- pnpm install -g @shipzone/npmci
|
|
||||||
- npmci npm prepare
|
|
||||||
|
|
||||||
# ====================
|
|
||||||
# security stage
|
|
||||||
# ====================
|
|
||||||
# ====================
|
|
||||||
# security stage
|
|
||||||
# ====================
|
|
||||||
auditProductionDependencies:
|
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
|
||||||
stage: security
|
|
||||||
script:
|
|
||||||
- npmci command npm config set registry https://registry.npmjs.org
|
|
||||||
- npmci command pnpm audit --audit-level=high --prod
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
allow_failure: true
|
|
||||||
|
|
||||||
auditDevDependencies:
|
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
|
||||||
stage: security
|
|
||||||
script:
|
|
||||||
- npmci command npm config set registry https://registry.npmjs.org
|
|
||||||
- npmci command pnpm audit --audit-level=high --dev
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
allow_failure: true
|
|
||||||
|
|
||||||
# ====================
|
|
||||||
# test stage
|
|
||||||
# ====================
|
|
||||||
|
|
||||||
testStable:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci node install stable
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
|
|
||||||
testBuild:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci node install stable
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm build
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
|
|
||||||
release:
|
|
||||||
stage: release
|
|
||||||
script:
|
|
||||||
- npmci node install stable
|
|
||||||
- npmci npm publish
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
# ====================
|
|
||||||
# metadata stage
|
|
||||||
# ====================
|
|
||||||
codequality:
|
|
||||||
stage: metadata
|
|
||||||
allow_failure: true
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
script:
|
|
||||||
- npmci command npm install -g typescript
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci npm install
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- priv
|
|
||||||
|
|
||||||
trigger:
|
|
||||||
stage: metadata
|
|
||||||
script:
|
|
||||||
- npmci trigger
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
pages:
|
|
||||||
stage: metadata
|
|
||||||
script:
|
|
||||||
- npmci node install stable
|
|
||||||
- npmci npm install
|
|
||||||
- npmci command npm run buildDocs
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
artifacts:
|
|
||||||
expire_in: 1 week
|
|
||||||
paths:
|
|
||||||
- public
|
|
||||||
allow_failure: true
|
|
57
changelog.md
Normal file
57
changelog.md
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
## 2025-01-18 - 2.1.0 - feat(ci)
|
||||||
|
Add GitHub Actions workflows for CI/CD
|
||||||
|
|
||||||
|
- Introduce GitHub Actions workflows to handle CI/CD processes for different events and branch types.
|
||||||
|
- Remove GitLab CI configuration in favor of GitHub Actions.
|
||||||
|
- Ensure security audits and tests are run as part of the CI pipeline.
|
||||||
|
|
||||||
|
## 2024-05-29 to 2024-03-30 - 2.0.0 - Configuration Updates
|
||||||
|
Improvements and updates to configuration files.
|
||||||
|
|
||||||
|
- Updated project description
|
||||||
|
- Modified TypeScript configuration
|
||||||
|
- Updated npmextra.json for githost details
|
||||||
|
|
||||||
|
## 2023-07-11 to 2023-07-10 - 2.0.0 - Organizational Change
|
||||||
|
Transitioned to a new organizational scheme for improved project structure.
|
||||||
|
|
||||||
|
- Implemented new organizational scheme
|
||||||
|
|
||||||
|
## 2023-01-12 - 1.0.29 - Breaking Change
|
||||||
|
Important breaking changes introduced, switched project to ECMAScript Module (ESM) format.
|
||||||
|
|
||||||
|
- Switched to ECMAScript Module (ESM)
|
||||||
|
- Core functionality updated
|
||||||
|
|
||||||
|
## 2021-09-17 to 2020-07-20 - 1.0.28 to 1.0.23 - Core Fixes
|
||||||
|
Multiple core improvements and bug fixes across several versions.
|
||||||
|
|
||||||
|
- Core functionality updates across versions
|
||||||
|
|
||||||
|
## 2018-03-09 to 2018-03-05 - 1.0.21 to 1.0.20 - Documentation Updates
|
||||||
|
Enhancements and updates to project documentation.
|
||||||
|
|
||||||
|
- License information updated in README
|
||||||
|
- General README updates
|
||||||
|
|
||||||
|
## 2018-03-05 - 1.0.19 - Standards Update
|
||||||
|
Updated project to comply with the latest coding standards.
|
||||||
|
|
||||||
|
- Applied latest standard updates
|
||||||
|
|
||||||
|
## 2018-03-03 - 1.0.18 - Initial Working Version
|
||||||
|
Initial release with all dependencies configured.
|
||||||
|
|
||||||
|
- First working project version with dependencies
|
||||||
|
|
||||||
|
## 2017-10-16 - 1.0.2 - Documentation
|
||||||
|
Initial project documentation added.
|
||||||
|
|
||||||
|
- Added initial README file
|
||||||
|
|
||||||
|
## 2017-10-16 - 1.0.1 - Initial Release
|
||||||
|
The initial release of the project.
|
||||||
|
|
||||||
|
- Project setup and initial commit
|
@ -5,16 +5,19 @@
|
|||||||
"githost": "code.foss.global",
|
"githost": "code.foss.global",
|
||||||
"gitscope": "push.rocks",
|
"gitscope": "push.rocks",
|
||||||
"gitrepo": "logcontext",
|
"gitrepo": "logcontext",
|
||||||
"description": "A module to enrich logs with context, featuring async log contexts and scope management.",
|
"description": "A module providing advanced asynchronous context management to enrich logs with context and manage scope effectively in Node.js applications.",
|
||||||
"npmPackagename": "@push.rocks/logcontext",
|
"npmPackagename": "@push.rocks/logcontext",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"logging",
|
"logging",
|
||||||
|
"context management",
|
||||||
"context enrichment",
|
"context enrichment",
|
||||||
"async log contexts",
|
"asynchronous",
|
||||||
"scope management",
|
"scope management",
|
||||||
"typescript",
|
"typescript",
|
||||||
"nodejs"
|
"nodejs",
|
||||||
|
"module",
|
||||||
|
"async log contexts"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
46
package.json
46
package.json
@ -1,9 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/logcontext",
|
"name": "@push.rocks/smartcontext",
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"description": "A module to enrich logs with context, featuring async log contexts and scope management.",
|
"description": "A module providing advanced asynchronous context management to enrich logs with context and manage scope effectively in Node.js applications.",
|
||||||
"main": "dist_ts/index.js",
|
"exports": {
|
||||||
"typings": "dist_ts/index.d.ts",
|
".": "./dist_ts/index.js"
|
||||||
|
},
|
||||||
|
"type": "module",
|
||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -12,19 +14,20 @@
|
|||||||
"buildDocs": "tsdoc"
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.27",
|
"@git.zone/tsbuild": "^2.1.27",
|
||||||
"@gitzone/tsbundle": "^2.0.7",
|
"@git.zone/tsbundle": "^2.0.7",
|
||||||
"@gitzone/tsrun": "^1.2.39",
|
"@git.zone/tsrun": "^1.2.39",
|
||||||
"@gitzone/tstest": "^1.0.57",
|
"@git.zone/tstest": "^1.0.57",
|
||||||
"@pushrocks/smartdelay": "^2.0.13",
|
"@push.rocks/smartdelay": "^3.0.5",
|
||||||
"@pushrocks/tapbundle": "^5.0.4",
|
"@push.rocks/tapbundle": "^5.0.4",
|
||||||
"@types/node": "^18.11.18"
|
"@types/node": "^18.11.18"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/lik": "^6.0.0",
|
"@push.rocks/lik": "^6.0.0",
|
||||||
"@pushrocks/smartcls": "^1.0.9",
|
"@push.rocks/smartcls": "^1.0.9",
|
||||||
"@pushrocks/smartunique": "^3.0.3",
|
"@push.rocks/smartunique": "^3.0.3",
|
||||||
"@types/shortid": "0.0.29"
|
"@types/shortid": "0.0.29",
|
||||||
|
"simple-async-context": "^0.0.13"
|
||||||
},
|
},
|
||||||
"private": false,
|
"private": false,
|
||||||
"browserslist": [
|
"browserslist": [
|
||||||
@ -42,18 +45,23 @@
|
|||||||
"npmextra.json",
|
"npmextra.json",
|
||||||
"readme.md"
|
"readme.md"
|
||||||
],
|
],
|
||||||
"type": "module",
|
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"logging",
|
"logging",
|
||||||
|
"context management",
|
||||||
"context enrichment",
|
"context enrichment",
|
||||||
"async log contexts",
|
"asynchronous",
|
||||||
"scope management",
|
"scope management",
|
||||||
"typescript",
|
"typescript",
|
||||||
"nodejs"
|
"nodejs",
|
||||||
|
"module",
|
||||||
|
"async log contexts"
|
||||||
],
|
],
|
||||||
"homepage": "https://code.foss.global/push.rocks/logcontext",
|
"homepage": "https://code.foss.global/push.rocks/logcontext#readme",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://code.foss.global/push.rocks/logcontext.git"
|
"url": "git+https://code.foss.global/push.rocks/logcontext.git"
|
||||||
|
},
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://code.foss.global/push.rocks/logcontext/issues"
|
||||||
}
|
}
|
||||||
}
|
}
|
11403
pnpm-lock.yaml
generated
11403
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
238
readme.md
238
readme.md
@ -1,76 +1,236 @@
|
|||||||
# @push.rocks/logcontext
|
# @push.rocks/smartcontext
|
||||||
enrich logs with context
|
|
||||||
|
A module to enrich logs with context, featuring async log contexts and scope management.
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
To incorporate @push.rocks/logcontext into your project, you will need Node.js and npm (Node Package Manager) installed. Once you have those prerequisites, you can install @push.rocks/logcontext by running the following command in your project's root directory:
|
|
||||||
|
|
||||||
```shell
|
To install the `@push.rocks/smartcontext` module, you can use npm. Make sure you have Node.js and npm installed on your system. Navigate to your project directory and run the following command:
|
||||||
npm install @push.rocks/logcontext --save
|
|
||||||
|
```bash
|
||||||
|
npm install @push.rocks/smartcontext
|
||||||
```
|
```
|
||||||
|
|
||||||
This command will download and install @push.rocks/logcontext and its dependencies into your project's `node_modules` folder and save it as a dependency in your project's `package.json` file.
|
This command will download and install the module and its dependencies into your project's `node_modules` directory.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
@push.rocks/logcontext is a TypeScript-focused module designed to enrich logging operations with contextual information, making it easier to trace logs through asynchronous operations in Node.js applications. It leverages modern JavaScript features such as async/await and provides a structured way to append and manage log-specific context.
|
The `@push.rocks/smartcontext` module provides an efficient way to enrich your logging with contextual information. It features asynchronous log contexts and scope management, especially useful in complex asynchronous workflows in Node.js applications. This documentation will guide you through its core components: `AsyncContext` and `AsyncStore`.
|
||||||
|
|
||||||
### Getting Started
|
### Setting Up and Basic Usage
|
||||||
|
|
||||||
First, ensure that you import the `Logger` class from @push.rocks/logcontext at the beginning of your TypeScript file:
|
First, import the necessary classes from the module in your TypeScript application:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { Logger } from "@push.rocks/logcontext";
|
import { AsyncContext, AsyncStore } from '@push.rocks/smartcontext';
|
||||||
```
|
```
|
||||||
|
|
||||||
### Creating a Logger Instance
|
#### Creating a Context
|
||||||
|
|
||||||
To start logging with context, you'll first need to create an instance of a logger. Optionally, you can provide a namespace string during instantiation to differentiate logs from different parts of your application.
|
You can create an `AsyncContext` for managing scope-based data sharing. Each context maintains a top-level `AsyncStore` instance. Let’s create a simple context and store some data:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
const appLogger = new Logger("appNamespace");
|
const mainContext = new AsyncContext();
|
||||||
|
|
||||||
|
// Add some data to the store
|
||||||
|
mainContext.store.add('username', 'john_doe');
|
||||||
|
mainContext.store.add('role', 'admin');
|
||||||
|
|
||||||
|
console.log(mainContext.store.get('username')); // Outputs: 'john_doe'
|
||||||
|
console.log(mainContext.store.get('role')); // Outputs: 'admin'
|
||||||
```
|
```
|
||||||
|
|
||||||
If no namespace is provided, a unique ID will be generated to serve as the namespace.
|
This `mainContext` acts as a container for contextual data throughout the lifecycle of your application or a specific code block.
|
||||||
|
|
||||||
### Adding Context and Logging
|
### Scoping with `runScoped`
|
||||||
|
|
||||||
One of the core features of @push.rocks/logcontext is the ability to associate contextual data with logs. This is very useful in asynchronous operations where tracing the flow of execution through different contexts is necessary.
|
The `runScoped` method allows you to run a function within a specific child `AsyncStore` scope. This is vital for isolating child data during asynchronous operations, while still having access to any relevant parent data:
|
||||||
|
|
||||||
#### Scoping Contexts
|
|
||||||
|
|
||||||
To scope a context, use the `scope` function of the Logger instance. This function takes an async function as an argument, and any logs emitted within this function will include the provided scoped context.
|
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
appLogger.scope(async () => {
|
await mainContext.runScoped(async (childStore: AsyncStore) => {
|
||||||
appLogger.addData("userId", 123);
|
childStore.add('transactionId', 'txn_123456');
|
||||||
appLogger.log("User authenticated");
|
|
||||||
// Further async operations where logs should include { userId: 123 }
|
console.log(childStore.get('transactionId')); // Outputs: 'txn_123456'
|
||||||
|
// Child store can also access data from the parent context:
|
||||||
|
console.log(childStore.get('username')); // Outputs: 'john_doe'
|
||||||
|
console.log(childStore.get('role')); // Outputs: 'admin'
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Asynchronous Context Propagation
|
Changes made within this child store do not leak back to the parent unless explicitly intended.
|
||||||
|
|
||||||
The context you define will propagate through asynchronous calls, ensuring that logs emitted after asynchronous operations still contain the context that was active when the operation started.
|
#### Isolating Data in Scoped Functions
|
||||||
|
|
||||||
#### Available Logging Methods
|
Data added within a child scope is only stored locally, unless you decide to propagate it to the parent. Additionally, you can delete data in the child without affecting the parent:
|
||||||
|
|
||||||
@push.rocks/logcontext exposes several logging methods, including `log`, `error`, `warn`, `info`, and `debug`. These methods are intended to mirror the conventional logging levels, making it easier to integrate into existing logging strategies.
|
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
appLogger.log("A standard log message.");
|
await mainContext.runScoped(async (childStore: AsyncStore) => {
|
||||||
appLogger.error("An error message.");
|
childStore.add('temporaryData', 'tempValue');
|
||||||
appLogger.warn("A warning message.");
|
console.log(childStore.get('temporaryData')); // Outputs: 'tempValue'
|
||||||
appLogger.info("An informational message.");
|
|
||||||
appLogger.debug("A debug message.");
|
// Delete data in child scope
|
||||||
|
childStore.delete('temporaryData');
|
||||||
|
console.log(childStore.get('temporaryData')); // Outputs: undefined
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
### Integrating with Third-Party Loggers
|
In this example, `temporaryData` is never propagated to the parent. Once deleted within the scope, it’s gone for that child context.
|
||||||
|
|
||||||
While @push.rocks/logcontext works well on its own for adding context to logs, it also supports integration with third-party logging solutions. You can associate a third-party logger using the `addThirdPartyLogger` method. This allows you to leverage the structured logging features of @push.rocks/logcontext while utilizing the logging infrastructure provided by another package.
|
### Advanced Usage
|
||||||
|
|
||||||
### Conclusion
|
Below are some patterns to demonstrate more advanced scenarios.
|
||||||
|
|
||||||
@push.rocks/logcontext offers a powerful and flexible solution for enriched logging in Node.js applications. By enabling contextual logging and integrating seamlessly with asynchronous operations, it facilitates better traceability and debugging of applications. Whether used standalone or alongside other logging libraries, @push.rocks/logcontext helps maintain clear and useful logs in complex applications.
|
#### Handling Complex Asynchronous Flows
|
||||||
|
|
||||||
|
Consider a scenario where you process a list of user requests asynchronously, assigning each request its own temporary data:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { AsyncContext } from '@push.rocks/smartcontext';
|
||||||
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
|
async function processUserRequests(users: string[], asyncContext: AsyncContext) {
|
||||||
|
for (const user of users) {
|
||||||
|
await asyncContext.runScoped(async (childStore: AsyncStore) => {
|
||||||
|
// Assign a unique request ID
|
||||||
|
const requestId = uuidv4();
|
||||||
|
childStore.add('currentUser', user);
|
||||||
|
childStore.add('requestId', requestId);
|
||||||
|
|
||||||
|
console.log(`Processing user: ${childStore.get('currentUser')} with Request ID: ${childStore.get('requestId')}`);
|
||||||
|
|
||||||
|
// Simulate an async operation
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||||
|
|
||||||
|
console.log(`Completed processing for user: ${childStore.get('currentUser')}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const mainContext = new AsyncContext();
|
||||||
|
processUserRequests(['alice', 'bob'], mainContext);
|
||||||
|
```
|
||||||
|
|
||||||
|
Each user’s scope remains neatly contained while still sharing any parent data (if present). This is highly beneficial in servers that handle multiple requests or tasks in parallel.
|
||||||
|
|
||||||
|
#### Sharing Data Across Scopes
|
||||||
|
|
||||||
|
Occasionally, you may want the child scope to add or modify data that persists in the parent. By default, `childStore` only extends the parent for lookup; changes remain local to the child. If you need truly shared data, you can write data directly to the parent store as well:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const newContext = new AsyncContext();
|
||||||
|
await newContext.runScoped(async (childStore: AsyncStore) => {
|
||||||
|
// This affects only the child store
|
||||||
|
childStore.add('childMessage', 'I am a child.');
|
||||||
|
|
||||||
|
// Directly manipulate the parent store if needed:
|
||||||
|
newContext.store.add('parentMessage', 'I am the parent (updated).');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Checking the parent store:
|
||||||
|
console.log(newContext.store.get('parentMessage')); // Outputs: 'I am the parent (updated)'
|
||||||
|
console.log(newContext.store.get('childMessage')); // Outputs: undefined (child-only)
|
||||||
|
```
|
||||||
|
|
||||||
|
This approach helps you maintain clarity on what data belongs to the parent vs. a transient child operation.
|
||||||
|
|
||||||
|
#### Parallel Scopes
|
||||||
|
|
||||||
|
When dealing with parallel asynchronous operations, each scope can be maintained independently. For instance, multiple login operations can be handled by the same parent context yet remain separate at runtime:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { AsyncContext } from '@push.rocks/smartcontext';
|
||||||
|
|
||||||
|
const userContext = new AsyncContext();
|
||||||
|
userContext.store.add('appVersion', '1.2.0');
|
||||||
|
|
||||||
|
async function handleUserLogin(userId: string, context: AsyncContext) {
|
||||||
|
await context.runScoped(async (childStore: AsyncStore) => {
|
||||||
|
childStore.add('sessionUser', userId);
|
||||||
|
|
||||||
|
console.log(`User: ${childStore.get('sessionUser')}, App: ${childStore.get('appVersion')}`);
|
||||||
|
// More processing for the user...
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
handleUserLogin('user123', userContext),
|
||||||
|
handleUserLogin('user456', userContext),
|
||||||
|
]);
|
||||||
|
```
|
||||||
|
|
||||||
|
In this example, the `userContext` store data (like `appVersion`) is accessible within each child scope, but each user session remains isolated. The parent’s data is shared and read-only for the children, unless children write specifically to the parent context.
|
||||||
|
|
||||||
|
#### Error Handling and Context-Enriched Logging
|
||||||
|
|
||||||
|
By leveraging contextual data, your logs can capture additional diagnostic details automatically:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
async function performTaskWithLogging(taskId: string, context: AsyncContext) {
|
||||||
|
try {
|
||||||
|
await context.runScoped(async (childStore: AsyncStore) => {
|
||||||
|
childStore.add('taskId', taskId);
|
||||||
|
// Simulate some failing operation
|
||||||
|
throw new Error('Task failed due to unexpected error');
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[Error] Task ID: ${context.store.get('taskId')}, Error: ${error.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const loggingContext = new AsyncContext();
|
||||||
|
performTaskWithLogging('task_789', loggingContext);
|
||||||
|
```
|
||||||
|
|
||||||
|
In the example above, we track `taskId` in the context store, ensuring that any error logs or additional logging statements automatically include that identifying information.
|
||||||
|
|
||||||
|
### Managing Contexts Across Modules
|
||||||
|
|
||||||
|
In large applications, you might want different modules to read or update shared context data without tight coupling between modules. One pattern is to define an `AsyncContext` in a root or shared location and pass it as needed:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// main.ts
|
||||||
|
import { AsyncContext } from '@push.rocks/smartcontext';
|
||||||
|
import { userModule } from './userModule';
|
||||||
|
import { billingModule } from './billingModule';
|
||||||
|
|
||||||
|
const globalContext = new AsyncContext();
|
||||||
|
globalContext.store.add('appName', 'SmartContextApp');
|
||||||
|
|
||||||
|
userModule(globalContext);
|
||||||
|
billingModule(globalContext);
|
||||||
|
```
|
||||||
|
|
||||||
|
Then, each module file can utilize that shared context:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// userModule.ts
|
||||||
|
export async function userModule(context: AsyncContext) {
|
||||||
|
await context.runScoped(async (childStore) => {
|
||||||
|
childStore.add('moduleName', 'UserModule');
|
||||||
|
console.log(`[UserModule] appName: ${childStore.get('appName')}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// billingModule.ts
|
||||||
|
export async function billingModule(context: AsyncContext) {
|
||||||
|
await context.runScoped(async (childStore) => {
|
||||||
|
childStore.add('moduleName', 'BillingModule');
|
||||||
|
console.log(`[BillingModule] appName: ${childStore.get('appName')}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
This approach keeps the code cleaner and ensures each module has access to shared data while retaining isolation where needed.
|
||||||
|
|
||||||
|
`@push.rocks/smartcontext` equips developers with a sophisticated asynchronous context management system, helping maintain clarity and consistency in logging and context sharing. Here’s a recap of the benefits:
|
||||||
|
|
||||||
|
- **Scoped Data**: Run logical operations within isolated child scopes, reducing contamination of global or parent-level data.
|
||||||
|
- **Asynchronous Safety**: Each asynchronous operation can track its own context, preventing race conditions on shared state.
|
||||||
|
- **Enhanced Logging**: Include relevant contextual data in your logs automatically for easier debugging and tracing.
|
||||||
|
- **Module-Friendly**: Share contexts without introducing deep module interdependencies.
|
||||||
|
|
||||||
|
These features simplify building robust Node.js applications, where concurrency and context tracking can otherwise become unwieldy. Enjoy exploring the breadth of this module’s functionality, and tailor it to meet your project’s unique challenges.
|
||||||
|
|
||||||
## License and Legal Information
|
## License and Legal Information
|
||||||
|
|
||||||
@ -85,7 +245,7 @@ This project is owned and maintained by Task Venture Capital GmbH. The names and
|
|||||||
### Company Information
|
### Company Information
|
||||||
|
|
||||||
Task Venture Capital GmbH
|
Task Venture Capital GmbH
|
||||||
Registered at District court Bremen HRB 35230 HB, Germany
|
Registered at District Court Bremen HRB 35230 HB, Germany
|
||||||
|
|
||||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||||
|
|
||||||
|
101
test/test.ts
101
test/test.ts
@ -1,41 +1,88 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { tap, expect } from '@push.rocks/tapbundle';
|
||||||
import * as logcontext from '../ts/index.js';
|
import { AsyncContext } from '../ts/logcontext.classes.asynccontext.js';
|
||||||
|
import { AsyncStore } from '../ts/logcontext.classes.asyncstore.js';
|
||||||
|
|
||||||
let testLogger = new logcontext.Logger('testNamespace');
|
/**
|
||||||
|
* This test file demonstrates how to use the AsyncContext and ensures
|
||||||
|
* that runScoped() properly creates child AsyncStore contexts and merges parent data.
|
||||||
|
*/
|
||||||
|
|
||||||
tap.test('should log for .error()', async () => {
|
const parentContext = new AsyncContext();
|
||||||
testLogger.error('first error message');
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should log for .fatal()', async () => {
|
tap.test('should run a scoped function and add data to a child store', async () => {
|
||||||
testLogger.fatal('this is fatal');
|
// add some default data to the parent store
|
||||||
});
|
parentContext.store.add('parentKey', 'parentValue');
|
||||||
|
expect(parentContext.store.get('parentKey')).toEqual('parentValue');
|
||||||
|
|
||||||
// set up independent log context
|
// now run a child scope, add some data, and check that parent's data is still accessible
|
||||||
tap.testParallel('should create an async LogContext', async (tools) => {
|
await parentContext.runScoped(async (childStore: AsyncStore) => {
|
||||||
testLogger.scope(async () => {
|
childStore.add('childKey', 'childValue');
|
||||||
testLogger.logmap.addData('paramName1', {
|
|
||||||
someData: 'someValue',
|
// child should see its own data
|
||||||
});
|
expect(childStore.get('childKey')).toEqual('childValue');
|
||||||
await tools.delayFor(10).then(async () => {
|
// child should also see parent data
|
||||||
testLogger.log('hi');
|
expect(childStore.get('parentKey')).toEqual('parentValue');
|
||||||
testLogger.error('custom error message');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.testParallel('should create a new scope', async () => {
|
tap.test('should not contaminate the parent store with child-only data', async () => {
|
||||||
testLogger.scope(async () => {
|
// create a new child scope
|
||||||
testLogger.logmap.addData('id1', {
|
await parentContext.runScoped(async (childStore: AsyncStore) => {
|
||||||
someData: 'otherValue',
|
childStore.add('temporaryKey', 'temporaryValue');
|
||||||
|
expect(childStore.get('temporaryKey')).toEqual('temporaryValue');
|
||||||
});
|
});
|
||||||
testLogger.info('anything');
|
// after the child scope finishes, 'temporaryKey' should not exist in the parent
|
||||||
|
expect(parentContext.store.get('temporaryKey')).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should allow adding data in multiple scopes independently', async () => {
|
||||||
|
// add data in first scope
|
||||||
|
await parentContext.runScoped(async (childStore: AsyncStore) => {
|
||||||
|
childStore.add('childKey1', 'childValue1');
|
||||||
|
expect(childStore.get('childKey1')).toEqual('childValue1');
|
||||||
|
});
|
||||||
|
|
||||||
|
// add data in second scope
|
||||||
|
await parentContext.runScoped(async (childStore: AsyncStore) => {
|
||||||
|
childStore.add('childKey2', 'childValue2');
|
||||||
|
expect(childStore.get('childKey2')).toEqual('childValue2');
|
||||||
|
});
|
||||||
|
|
||||||
|
// neither childKey1 nor childKey2 should exist in the parent store
|
||||||
|
expect(parentContext.store.get('childKey1')).toBeUndefined();
|
||||||
|
expect(parentContext.store.get('childKey2')).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should allow deleting data in a child store without removing it from the parent store', async () => {
|
||||||
|
// ensure parent has some data
|
||||||
|
parentContext.store.add('deletableKey', 'iShouldStayInParent');
|
||||||
|
|
||||||
|
await parentContext.runScoped(async (childStore: AsyncStore) => {
|
||||||
|
// child sees the parent's data
|
||||||
|
expect(childStore.get('deletableKey')).toEqual('iShouldStayInParent');
|
||||||
|
// attempt to delete it in the child
|
||||||
|
childStore.delete('deletableKey');
|
||||||
|
// child no longer sees it
|
||||||
|
expect(childStore.get('deletableKey')).toBeUndefined();
|
||||||
|
// but parent still has it
|
||||||
|
expect(parentContext.store.get('deletableKey')).toEqual('iShouldStayInParent');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should log within default scope', async (toolsArg) => {
|
tap.test('should allow multiple child scopes to share the same parent store data', async () => {
|
||||||
await toolsArg.delayFor(3000);
|
// add a key to the parent store
|
||||||
testLogger.log('message without context');
|
parentContext.store.add('sharedKey', 'sharedValue');
|
||||||
|
expect(parentContext.store.get('sharedKey')).toEqual('sharedValue');
|
||||||
|
|
||||||
|
// first child scope
|
||||||
|
await parentContext.runScoped(async (firstChild: AsyncStore) => {
|
||||||
|
expect(firstChild.get('sharedKey')).toEqual('sharedValue');
|
||||||
|
});
|
||||||
|
|
||||||
|
// second child scope
|
||||||
|
await parentContext.runScoped(async (secondChild: AsyncStore) => {
|
||||||
|
expect(secondChild.get('sharedKey')).toEqual('sharedValue');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
@ -1,8 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* autocreated commitinfo by @pushrocks/commitinfo
|
* autocreated commitinfo by @push.rocks/commitinfo
|
||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@pushrocks/logcontext',
|
name: '@push.rocks/smartcontext',
|
||||||
version: '2.0.0',
|
version: '2.1.0',
|
||||||
description: 'enrich logs with context'
|
description: 'A module providing advanced asynchronous context management to enrich logs with context and manage scope effectively in Node.js applications.'
|
||||||
}
|
}
|
||||||
|
@ -1 +1 @@
|
|||||||
export * from './logcontext.classes.logger.js';
|
export * from './logcontext.classes.asynccontext.js';
|
||||||
|
13
ts/logcontext.classes.asynccontext.ts
Normal file
13
ts/logcontext.classes.asynccontext.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import * as plugins from './logcontext.plugins.js';
|
||||||
|
import { AsyncStore } from './logcontext.classes.asyncstore.js';
|
||||||
|
|
||||||
|
export class AsyncContext {
|
||||||
|
private context = new plugins.simpleAsyncContext.AsyncContext.Variable<AsyncStore>();
|
||||||
|
public store = new AsyncStore();
|
||||||
|
public runScoped(functionArg: (storeArg: AsyncStore) => Promise<void>) {
|
||||||
|
this.context.run(this.store, async () => {
|
||||||
|
const childStore = new AsyncStore(this.store);
|
||||||
|
functionArg(childStore)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
50
ts/logcontext.classes.asyncstore.ts
Normal file
50
ts/logcontext.classes.asyncstore.ts
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import * as plugins from './logcontext.plugins.js';
|
||||||
|
|
||||||
|
export class AsyncStore {
|
||||||
|
private parentStore?: AsyncStore;
|
||||||
|
private deletedKeys: string[] = [];
|
||||||
|
private dataObject: {[key: string]: any} = {};
|
||||||
|
|
||||||
|
constructor(parentStore?: AsyncStore) {
|
||||||
|
this.parentStore = parentStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
private cleanUp() {
|
||||||
|
for (const key of this.deletedKeys) {
|
||||||
|
if (this.parentStore && this.parentStore.get(key)) {
|
||||||
|
// ok still valid
|
||||||
|
} else {
|
||||||
|
delete this.deletedKeys[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public add(keyArg: string, objectArg: any) {
|
||||||
|
this.cleanUp();
|
||||||
|
if (this.deletedKeys.includes(keyArg)) {
|
||||||
|
this.deletedKeys = this.deletedKeys.filter((key) => key !== keyArg);
|
||||||
|
}
|
||||||
|
this.dataObject[keyArg] = objectArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public delete(paramName: string) {
|
||||||
|
this.cleanUp();
|
||||||
|
if (this.parentStore.get(paramName)) {
|
||||||
|
this.deletedKeys.push(paramName);
|
||||||
|
}
|
||||||
|
delete this.dataObject[paramName];
|
||||||
|
}
|
||||||
|
|
||||||
|
public get(paramName: string) {
|
||||||
|
this.cleanUp();
|
||||||
|
if (this.deletedKeys.includes(paramName)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return this.dataObject[paramName] || this.parentStore?.get(paramName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getAll() {
|
||||||
|
this.cleanUp();
|
||||||
|
return {...this.dataObject, ...(this.parentStore?.getAll() || {})};
|
||||||
|
}
|
||||||
|
}
|
@ -1,123 +0,0 @@
|
|||||||
import * as plugins from './logcontext.plugins.js';
|
|
||||||
import { LogMap } from './logcontext.classes.logmap.js';
|
|
||||||
|
|
||||||
export class Logger {
|
|
||||||
public namespaceString: string;
|
|
||||||
smartcls: plugins.smartcls.SmartCls;
|
|
||||||
public logmap: LogMap;
|
|
||||||
public thirdPartyLogger: any;
|
|
||||||
settings = {
|
|
||||||
enableScope: () => {
|
|
||||||
this.settingsParams.scope = true;
|
|
||||||
},
|
|
||||||
disableScope: () => {
|
|
||||||
this.settingsParams.scope = false;
|
|
||||||
},
|
|
||||||
enableAddData: () => {
|
|
||||||
this.settingsParams.addData = true;
|
|
||||||
},
|
|
||||||
disableAddData: () => {
|
|
||||||
this.settingsParams.addData = false;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
private settingsParams: { scope: boolean; addData: boolean } = {
|
|
||||||
scope: true,
|
|
||||||
addData: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
constructor(namespaceArg: string = plugins.smartunique.shortId()) {
|
|
||||||
this.namespaceString = namespaceArg;
|
|
||||||
this.smartcls = new plugins.smartcls.SmartCls();
|
|
||||||
this.logmap = new LogMap(this.smartcls);
|
|
||||||
}
|
|
||||||
|
|
||||||
addData(paramNameArg: string, dataArg: any) {
|
|
||||||
if (this.settingsParams.addData) {
|
|
||||||
this.logmap.addData(paramNameArg, dataArg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
addThirdPartyLogger(thirdPartyLoggerArg: any) {
|
|
||||||
this.thirdPartyLogger = thirdPartyLoggerArg;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* debug
|
|
||||||
* @param logMessageArg
|
|
||||||
*/
|
|
||||||
debug(logMessageArg: string) {
|
|
||||||
this.routeLog('debug', logMessageArg);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* log
|
|
||||||
* @param logMessageArg
|
|
||||||
*/
|
|
||||||
log(logMessageArg: string) {
|
|
||||||
this.routeLog('log', logMessageArg);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* info
|
|
||||||
* @param logMessageArg
|
|
||||||
*/
|
|
||||||
info(logMessageArg: string) {
|
|
||||||
this.routeLog('info', logMessageArg);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* error
|
|
||||||
* @param logMessageArg
|
|
||||||
* @param args
|
|
||||||
*/
|
|
||||||
error(logMessageArg: string, ...args: any) {
|
|
||||||
this.routeLog('error', logMessageArg, ...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* warn
|
|
||||||
* @param logMessageArg
|
|
||||||
* @param args
|
|
||||||
*/
|
|
||||||
warn(logMessageArg: string, ...args: any) {
|
|
||||||
this.routeLog('warn', logMessageArg, ...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* fatal
|
|
||||||
* @param logMessageArg
|
|
||||||
* @param args
|
|
||||||
*/
|
|
||||||
fatal(logMessageArg: string, ...args: any) {
|
|
||||||
this.routeLog('fatal', logMessageArg, ...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
// creates a new async scope
|
|
||||||
scope(funcArg: any) {
|
|
||||||
// create node continuation scope
|
|
||||||
if (this.settingsParams.scope) {
|
|
||||||
this.smartcls.run(funcArg);
|
|
||||||
} else {
|
|
||||||
funcArg();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* routes the log according to whats available in the environment
|
|
||||||
* @param {string} logMethod
|
|
||||||
* @param {any} messageArg
|
|
||||||
* @param {any[]} ...args
|
|
||||||
*/
|
|
||||||
private routeLog(logMethod: string, messageArg: string, ...args: any) {
|
|
||||||
const logObject = {
|
|
||||||
message: messageArg,
|
|
||||||
type: logMethod,
|
|
||||||
logContext: this.logmap.getAllData(),
|
|
||||||
};
|
|
||||||
if (this.thirdPartyLogger && this.thirdPartyLogger[logMethod]) {
|
|
||||||
this.thirdPartyLogger[logMethod](logObject, ...args);
|
|
||||||
} else {
|
|
||||||
console.log(logObject);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
import * as plugins from './logcontext.plugins.js';
|
|
||||||
|
|
||||||
export class LogMap {
|
|
||||||
smartcls: plugins.smartcls.SmartCls;
|
|
||||||
paramMap = new plugins.lik.Stringmap();
|
|
||||||
|
|
||||||
constructor(clsNamespaceArg: plugins.smartcls.SmartCls) {
|
|
||||||
this.smartcls = clsNamespaceArg;
|
|
||||||
}
|
|
||||||
|
|
||||||
addData(paramName: string, logData: any) {
|
|
||||||
this.paramMap.addString(paramName);
|
|
||||||
this.smartcls.set(paramName, logData);
|
|
||||||
}
|
|
||||||
|
|
||||||
deleteData(paramName: string) {
|
|
||||||
this.smartcls.set(paramName, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
getData(paramName: string) {
|
|
||||||
return this.smartcls.get(paramName);
|
|
||||||
}
|
|
||||||
|
|
||||||
getAllData() {
|
|
||||||
const returnObject: any = {};
|
|
||||||
for (const stringArg of this.paramMap.getStringArray()) {
|
|
||||||
returnObject[stringArg] = this.smartcls.get(stringArg);
|
|
||||||
}
|
|
||||||
return returnObject;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +1,10 @@
|
|||||||
// native scope
|
|
||||||
import { AsyncLocalStorage } from 'async_hooks';
|
|
||||||
|
|
||||||
export { AsyncLocalStorage };
|
|
||||||
|
|
||||||
// pushrocks scope
|
// pushrocks scope
|
||||||
import * as lik from '@pushrocks/lik';
|
import * as lik from '@push.rocks/lik';
|
||||||
import * as smartcls from '@pushrocks/smartcls';
|
import * as smartunique from '@push.rocks/smartunique';
|
||||||
import * as smartunique from '@pushrocks/smartunique';
|
|
||||||
|
|
||||||
export { lik, smartcls, smartunique };
|
export { lik, smartunique };
|
||||||
|
|
||||||
|
// third party scope
|
||||||
|
import simpleAsyncContext from 'simple-async-context';
|
||||||
|
|
||||||
|
export { simpleAsyncContext };
|
@ -6,7 +6,9 @@
|
|||||||
"module": "NodeNext",
|
"module": "NodeNext",
|
||||||
"moduleResolution": "NodeNext",
|
"moduleResolution": "NodeNext",
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"verbatimModuleSyntax": true
|
"verbatimModuleSyntax": true,
|
||||||
|
"baseUrl": ".",
|
||||||
|
"paths": {}
|
||||||
},
|
},
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"dist_*/**/*.d.ts"
|
"dist_*/**/*.d.ts"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user