Skip to content

Commit 3b33087

Browse files
committed
feat: add setLogger
1 parent a89d9b6 commit 3b33087

File tree

10 files changed

+139
-2
lines changed

10 files changed

+139
-2
lines changed

docs/api-js/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
- [DownloadProgressCallback](type-aliases/DownloadProgressCallback.md)
3131
- [HandleBinaryVersionMismatchCallback](type-aliases/HandleBinaryVersionMismatchCallback.md)
32+
- [LoggerFunction](type-aliases/LoggerFunction.md)
3233
- [SyncStatusChangedCallback](type-aliases/SyncStatusChangedCallback.md)
3334

3435
## Variables
@@ -48,8 +49,10 @@
4849
- [getUpdateMetadata](functions/getUpdateMetadata.md)
4950
- [notifyAppReady](functions/notifyAppReady.md)
5051
- [resetClientUniqueId](functions/resetClientUniqueId.md)
52+
- [resetLogger](functions/resetLogger.md)
5153
- [restartApp](functions/restartApp.md)
5254
- [setDataTransmissionEnabled](functions/setDataTransmissionEnabled.md)
55+
- [setLogger](functions/setLogger.md)
5356
- [setLogLevel](functions/setLogLevel.md)
5457
- [setTelemetryEnabled](functions/setTelemetryEnabled.md)
5558
- [sync](functions/sync.md)

docs/api-js/enumerations/LogLevel.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
# Enumeration: LogLevel
88

9+
Used to categorize log messages by their severity and importance.
10+
911
## Enumeration Members
1012

1113
### DEBUG
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[**@appzung/react-native-code-push v11.0.0-rc5**](../README.md)
2+
3+
---
4+
5+
[@appzung/react-native-code-push](../README.md) / resetLogger
6+
7+
# Function: resetLogger()
8+
9+
> **resetLogger**(): `void`
10+
11+
Reset the logger to the default implementation
12+
13+
## Returns
14+
15+
`void`

docs/api-js/functions/setLogger.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[**@appzung/react-native-code-push v11.0.0-rc5**](../README.md)
2+
3+
---
4+
5+
[@appzung/react-native-code-push](../README.md) / setLogger
6+
7+
# Function: setLogger()
8+
9+
> **setLogger**(`logger`): `void`
10+
11+
Set a custom logger function to handle all CodePush logs
12+
13+
## Parameters
14+
15+
### logger
16+
17+
[`LoggerFunction`](../type-aliases/LoggerFunction.md)
18+
19+
A function that takes a log level and message and handles the logging
20+
21+
## Returns
22+
23+
`void`
24+
25+
## Example
26+
27+
```
28+
import { setLogger, LogLevel } from '@appzung/react-native-code-push';
29+
30+
// Custom logger that sends critical logs to a crash reporting service
31+
setLogger((level, message) => {
32+
// Always log to console
33+
console.log(`[CodePush] ${message}`);
34+
35+
// Send error logs to crash reporting
36+
if (level === LogLevel.ERROR) {
37+
MyCrashReportingService.log(`CodePush error: ${message}`);
38+
}
39+
});
40+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[**@appzung/react-native-code-push v11.0.0-rc5**](../README.md)
2+
3+
---
4+
5+
[@appzung/react-native-code-push](../README.md) / LoggerFunction
6+
7+
# Type Alias: LoggerFunction()
8+
9+
> **LoggerFunction**: (`level`, `message`) => `void`
10+
11+
## Parameters
12+
13+
### level
14+
15+
[`LogLevel`](../enumerations/LogLevel.md)
16+
17+
### message
18+
19+
`string`
20+
21+
## Returns
22+
23+
`void`

src/enums/LogLevel.enum.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Used to categorize log messages by their severity and importance.
3+
*/
14
export enum LogLevel {
25
DEBUG,
36
INFO,

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export * from './sync';
1111
export * from './getClientUniqueId';
1212
export * from './resetClientUniqueId';
1313
export * from './logLevel';
14+
export * from './logger';
1415
export * from './telemetry';
1516
export * from './dataTransmission';
1617

src/internals/logger.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { LogLevel } from '../enums/LogLevel.enum';
2+
3+
export type LoggerFunction = (level: LogLevel, message: string) => void;
4+
5+
const defaultLogger: LoggerFunction = (_level: LogLevel, message: string): void => {
6+
console.log(`[CodePush] ${message}`);
7+
};
8+
9+
let currentLogger: LoggerFunction = defaultLogger;
10+
11+
/**
12+
* Set a custom logger function to handle all CodePush logs
13+
*
14+
* @param logger A function that takes a log level and message and handles the logging
15+
* @example
16+
* ```
17+
* import { setLogger, LogLevel } from '@appzung/react-native-code-push';
18+
*
19+
* // Custom logger that sends critical logs to a crash reporting service
20+
* setLogger((level, message) => {
21+
* // Always log to console
22+
* console.log(`[CodePush] ${message}`);
23+
*
24+
* // Send error logs to crash reporting
25+
* if (level === LogLevel.ERROR) {
26+
* MyCrashReportingService.log(`CodePush error: ${message}`);
27+
* }
28+
* });
29+
* ```
30+
*/
31+
export const setLogger = (logger: LoggerFunction): void => {
32+
currentLogger = logger;
33+
};
34+
35+
/**
36+
* Get the current logger function
37+
* @returns The current logger function
38+
*/
39+
export const getLogger = (): LoggerFunction => currentLogger;
40+
41+
/**
42+
* Reset the logger to the default implementation
43+
*/
44+
export const resetLogger = (): void => {
45+
currentLogger = defaultLogger;
46+
};

src/internals/utils/log.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import type { LogLevel } from '../../enums/LogLevel.enum';
22
import { getLogLevel } from '../../logLevel';
3+
import { getLogger } from '../logger';
34

4-
/* Logs messages to console with the [CodePush] prefix */
5+
/* Logs messages using the configured logger */
56
export function log(level: LogLevel, message: string): void {
67
const currentLogLevel = getLogLevel();
78
if (level < currentLogLevel) {
89
return;
910
}
1011

11-
console.log(`[CodePush] ${message}`);
12+
const logger = getLogger();
13+
logger(level, message);
1214
}

src/logger.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { setLogger, resetLogger } from './internals/logger';
2+
export type { LoggerFunction } from './internals/logger';

0 commit comments

Comments
 (0)