Skip to content

Commit db87e7f

Browse files
feat: Add ENV var to use JSON logging (#4474)
Signed-off-by: kasey-alusi-vcc <[email protected]>
1 parent 7bf6405 commit db87e7f

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

.env.http.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,5 @@ OPERATOR_KEY_MAIN= # Operator private key used to sign transaction
114114
# MULTI_SET=false # Implementation for setting multiple K/V pairs
115115

116116
# ========== DEVELOPMENT & TESTING ==========
117-
# LOG_LEVEL=trace # Logging level (trace, debug, info, warn, error, fatal)
117+
# LOG_LEVEL=trace # Logging level (trace, debug, info, warn, error, fatal)
118+
# PRETTY_LOGS_ENABLED # Enable pino-pretty logging (true) or json logging (false)

.env.ws.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ SUBSCRIPTIONS_ENABLED=true # Must be true for the WebSocket server to func
6363
# CLIENT_TRANSPORT_SECURITY=false # Enable or disable TLS for both networks
6464
# USE_ASYNC_TX_PROCESSING=true # If true, returns tx hash immediately after prechecks
6565
# LOG_LEVEL=trace # Logging level (trace, debug, info, warn, error, fatal)
66-
66+
# PRETTY_LOGS_ENABLED # Enable pino-pretty logging (true) or json logging (false)

docs/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Unless you need to set a non-default value, it is recommended to only populate o
121121
| `HEDERA_NETWORK` | "" | Which network to connect to. Automatically populates the main node & mirror node endpoints. Can be `previewnet`, `testnet`, `mainnet` or a map of network IPs -> node accountIds e.g. `{"127.0.0.1:50211":"0.0.3"}` |
122122
| `INPUT_SIZE_LIMIT` | "1mb" | The [koa-jsonrpc](https://github.com/Bitclimb/koa-jsonrpc) maximum size allowed for requests |
123123
| `LOG_LEVEL` | "trace" | The logging level for the application. Valid values are `trace`, `debug`, `info`, `warn`, `error`, and `fatal`. |
124+
| `PRETTY_LOGS_ENABLED` | "true" | Controls the logging output format. When set to `true` (default), uses human-readable pretty format with colors. When set to `false`, uses structured JSON format for log aggregation systems. |
124125
| `MAX_BLOCK_RANGE` | "5" | The maximum block number greater than the mirror node's latest block to query for |
125126
| `OPERATOR_ID_MAIN` | "" | Operator account ID used to pay for transactions. In `S.R.N` format, e.g. `0.0.1001`. |
126127
| `OPERATOR_KEY_FORMAT` | "DER" | Operator private key format. Valid types are `DER`, `HEX_ECDSA`, or `HEX_ED25519` |

packages/config-service/src/services/globalConfig.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,11 @@ const _CONFIG = {
368368
required: false,
369369
defaultValue: 'trace',
370370
},
371+
PRETTY_LOGS_ENABLED: {
372+
type: 'boolean',
373+
required: false,
374+
defaultValue: true,
375+
},
371376
MAX_BLOCK_RANGE: {
372377
type: 'number',
373378
required: false,

packages/server/src/server.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { spec } from './koaJsonRpc/lib/RpcError';
1818
// https://nodejs.org/api/async_context.html#asynchronous-context-tracking
1919
const context = new AsyncLocalStorage<{ requestId: string }>();
2020

21+
const prettyLogsEnabled = ConfigService.get('PRETTY_LOGS_ENABLED');
22+
2123
const mainLogger = pino({
2224
name: 'hedera-json-rpc-relay',
2325
level: ConfigService.get('LOG_LEVEL'),
@@ -26,16 +28,19 @@ const mainLogger = pino({
2628
const store = context.getStore();
2729
return store ? { requestId: `[Request ID: ${store.requestId}] ` } : {};
2830
},
29-
transport: {
30-
target: 'pino-pretty',
31-
options: {
32-
colorize: true,
33-
translateTime: true,
34-
messageFormat: '{requestId}{msg}',
35-
// Ignore one or several keys, nested keys are supported with each property delimited by a dot character (`.`)
36-
ignore: 'requestId',
31+
// Use pino-pretty when PRETTY_LOGS_ENABLED is true (default), otherwise use JSON format
32+
...(prettyLogsEnabled && {
33+
transport: {
34+
target: 'pino-pretty',
35+
options: {
36+
colorize: true,
37+
translateTime: true,
38+
messageFormat: '{requestId}{msg}',
39+
// Ignore one or several keys, nested keys are supported with each property delimited by a dot character (`.`)
40+
ignore: 'requestId',
41+
},
3742
},
38-
},
43+
}),
3944
});
4045

4146
export const logger = mainLogger.child({ name: 'rpc-server' });

0 commit comments

Comments
 (0)