Skip to content

Commit fdb1a9f

Browse files
authored
Merge pull request #57 from shiftcode/#56-fix-provideLogRequestInfo
#56 fix provide log request info
2 parents aa450e3 + 0f4103c commit fdb1a9f

21 files changed

+194
-60
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Shows the mapping between the angular version and our lib versions.
2020

2121
| Angular Version | Lib Version |
2222
|-----------------|-------------------------------|
23-
| `^20` | `^11` |
23+
| `^20` | `^11 \|\| ^12` |
2424
| `^19` | `^7 \|\| ^8 \|\| ^9 \|\| ^10` |
2525
| `^18` | `^6` |
2626
| `^17` | `^5` |

libs/components/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@shiftcode/ngx-components",
3-
"version": "11.0.0",
3+
"version": "12.0.0-pr56.2",
44
"repository": "https://github.com/shiftcode/sc-ng-commons-public",
55
"license": "MIT",
66
"author": "shiftcode GmbH <[email protected]>",
@@ -25,7 +25,7 @@
2525
"@angular/forms": "^20.0.0",
2626
"@angular/router": "^20.0.0",
2727
"@shiftcode/logger": "^3.0.0",
28-
"@shiftcode/ngx-core": "^11.0.0 || ^11.0.0-pr50",
28+
"@shiftcode/ngx-core": "^12.0.0 || ^12.0.0-pr56",
2929
"rxjs": "^6.5.3 || ^7.4.0"
3030
}
3131
}

libs/core/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,27 @@ class MyDirective {
6060
- Node Console: `withNodeConsoleTransport(...)`
6161
- Remote: `withRemoteTransport(...)`
6262
- AWS CloudWatch: see `withCloudwatchTransport(...)`
63+
64+
### LogRequestInfo Integration
65+
You can enrich your log requests for `Remote` and `CloudWatch` Transports with custom information like user/session/device metadata using the `withRequestInfoFn` feature.
66+
67+
#### Usage Example
68+
Use the feature `withRequestInfoFn` function to provide a factory for a `LogRequestInfoFn`.
69+
The `LogRequestInfoFn` is a function itself that is called prior every log request. Its return value needs to be an object with the information to be included.
70+
71+
```ts
72+
import { LogLevel, provideLogger, withRemoteTransport, withRequestInfoFn, LogRequestInfo } from '@shiftcode/ngx-core'
73+
74+
bootstrapApplication(AppComponent, {
75+
providers: [
76+
provideLogger(
77+
withRemoteTransport({ logLevel: LogLevel.INFO, /* ...other config... */ }),
78+
withRequestInfoFn(() => {
79+
const authService = inject(AuthService)
80+
const router = inject(Router)
81+
return (): LogRequestInfo => ({ userId: authService.currentUser?.id, url: this.router.url })
82+
})
83+
),
84+
],
85+
})
86+
```

libs/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@shiftcode/ngx-core",
3-
"version": "11.1.0",
3+
"version": "12.0.0-pr56.4",
44
"repository": "https://github.com/shiftcode/sc-ng-commons-public",
55
"license": "MIT",
66
"author": "shiftcode GmbH <[email protected]>",

libs/core/src/lib/logger/cloudwatch/cloud-watch.service.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
/* eslint-disable no-console */
2-
import { Injectable, inject } from '@angular/core'
2+
import { inject, Injectable } from '@angular/core'
33
import { createJsonLogObjectData, LogLevel } from '@shiftcode/logger'
44
import { jsonMapSetStringifyReplacer } from '@shiftcode/utilities'
55
import { BehaviorSubject, concatMap, Observable, of, retry, Subject, throwError } from 'rxjs'
66
import { catchError, filter, map, mergeMap, shareReplay, take } from 'rxjs/operators'
77
import { CLOUD_WATCH_LOG_TRANSPORT_CONFIG } from './cloud-watch-log-transport-config.injection-token'
8-
import { CloudWatchLogTransportConfig } from './cloud-watch-log-transport-config.model'
98
import { HttpClient } from '@angular/common/http'
109
import { isLogStreamNotFoundError } from './is-error.function'
1110
import { ClientIdService } from '../../client-id/client-id.service'
1211
import { RemoteLogData } from '../remote/remote-log-data.model'
13-
import { LOG_REQUEST_INFO } from '../log-request-info.token'
12+
import { LOG_REQUEST_INFO_FN } from '../log-request-info-fn.token'
1413

1514
interface CloudWatchLogEvent {
1615
logStreamName: string
@@ -25,8 +24,8 @@ interface CloudWatchLogEvent {
2524
@Injectable({ providedIn: 'root' })
2625
export class CloudWatchService {
2726
private readonly httpClient = inject(HttpClient)
28-
private readonly config = inject<CloudWatchLogTransportConfig>(CLOUD_WATCH_LOG_TRANSPORT_CONFIG)
29-
private readonly logRequestInfoProvider = inject<Record<string, string>>(LOG_REQUEST_INFO, { optional: true })
27+
private readonly config = inject(CLOUD_WATCH_LOG_TRANSPORT_CONFIG)
28+
private readonly logRequestInfoFn = inject(LOG_REQUEST_INFO_FN, { optional: true })
3029

3130
private readonly retrying$ = new BehaviorSubject<boolean>(false)
3231
private readonly logStream$ = new Observable<void>()
@@ -62,7 +61,9 @@ export class CloudWatchService {
6261

6362
const logDataObject: RemoteLogData = {
6463
...createJsonLogObjectData(level, context, dTimestamp, args),
65-
requestInfo: this.logRequestInfoProvider ?? {},
64+
}
65+
if (this.logRequestInfoFn) {
66+
logDataObject.requestInfo = this.logRequestInfoFn()
6667
}
6768

6869
this.logsSubject.next({

libs/core/src/lib/logger/cloudwatch/with-cloudwatch-transport.function.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { LoggerFeature, LoggerFeatureKind } from '../provide-logger'
21
import { LogTransport } from '@shiftcode/logger'
32
import { CLOUD_WATCH_LOG_TRANSPORT_CONFIG } from './cloud-watch-log-transport-config.injection-token'
43
import { CloudWatchLogTransportConfig } from './cloud-watch-log-transport-config.model'
54
import { CloudWatchLogTransport } from './cloud-watch-log-transport.service'
5+
import { LoggerFeatureKind } from '../logger-feature-kind.enum'
6+
import { LoggerFeature } from '../logger-feature.type'
67

78
export function withCloudwatchTransport(
89
cloudWatchLogTransportConfigOrFactory: CloudWatchLogTransportConfig | (() => CloudWatchLogTransportConfig),

libs/core/src/lib/logger/console/with-browser-console-transport.function.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { LoggerFeature, LoggerFeatureKind } from '../provide-logger'
21
import { ConsoleLogTransportConfig } from './console-log-transport-config'
32
import { CONSOLE_LOG_TRANSPORT_CONFIG } from './console-log-transport-config.injection-token'
43
import { ConsoleLogTransport } from './console-log-transport.service'
54
import { LogTransport } from '@shiftcode/logger'
5+
import { LoggerFeatureKind } from '../logger-feature-kind.enum'
6+
import { LoggerFeature } from '../logger-feature.type'
67

78
export function withBrowserConsoleTransport(
89
consoleLoggerConfigOrFactory: ConsoleLogTransportConfig | (() => ConsoleLogTransportConfig),

libs/core/src/lib/logger/console/with-node-console-transport.function.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { LoggerFeature, LoggerFeatureKind } from '../provide-logger'
21
import { ConsoleLogTransportConfig } from './console-log-transport-config'
32
import { CONSOLE_LOG_TRANSPORT_CONFIG } from './console-log-transport-config.injection-token'
43
import { NodeConsoleLogTransport } from './node-console-log-transport.service'
54
import { LogTransport } from '@shiftcode/logger'
5+
import { LoggerFeatureKind } from '../logger-feature-kind.enum'
6+
import { LoggerFeature } from '../logger-feature.type'
67

78
export function withNodeConsoleTransport(
89
consoleLoggerConfigOrFactory: ConsoleLogTransportConfig | (() => ConsoleLogTransportConfig),
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { InjectionToken } from '@angular/core'
2+
import { LogRequestInfoFn } from './log-request-info-fn.type'
3+
4+
export const LOG_REQUEST_INFO_FN = new InjectionToken<LogRequestInfoFn>('LOG_REQUEST_INFO_FN')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export type LogRequestInfo = Record<string, string | undefined | null>
2+
3+
export interface LogRequestInfoFn {
4+
(): LogRequestInfo
5+
}

0 commit comments

Comments
 (0)