From 63b2226baa8b48307051c014d99d61c39eb17ba8 Mon Sep 17 00:00:00 2001 From: Norbert Kiesel Date: Fri, 26 Jul 2024 19:26:03 -0700 Subject: [PATCH] Added isoTimestamp option When set to true, it renders the timestamp in log entries using Date.toIsoString(), which is easier to read by humans than the default of milliseconds. --- API.md | 7 +++++++ lib/config.js | 1 + lib/core.js | 2 +- lib/types/server/options.d.ts | 7 +++++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/API.md b/API.md index 40d8ffe88..ffe62e42e 100755 --- a/API.md +++ b/API.md @@ -53,6 +53,13 @@ the framework. Cannot be set to `false` along with a [`port`](#server.options.port) value. +#### `server.options.isoTimestamp` + +Default value: `false`. + +If `true`, shows the log timestamp in ISO-8601 UTC format. If `false`, shows the log timestamp +in milliseconds since 1970-01-01. + #### `server.options.cache` Default value: `{ provider: { constructor: require('@hapi/catbox-memory'), options: { partition: 'hapi-cache' } } }`. diff --git a/lib/config.js b/lib/config.js index 2b668f97d..38251dfa7 100755 --- a/lib/config.js +++ b/lib/config.js @@ -239,6 +239,7 @@ internals.server = Validate.object({ address: Validate.string().hostname(), app: Validate.object().allow(null), autoListen: Validate.boolean(), + isoTimestamp: Validate.boolean().default(false), cache: Validate.allow(null), // Validated elsewhere compression: Validate.object({ minBytes: Validate.number().min(1).integer().default(1024) diff --git a/lib/core.js b/lib/core.js index 43329536f..2b3f183a4 100755 --- a/lib/core.js +++ b/lib/core.js @@ -660,7 +660,7 @@ exports = module.exports = internals.Core = class { tags = [tags]; } - const timestamp = Date.now(); + const timestamp = this.settings.isoTimestamp ? new Date(Date.now()).toISOString() : Date.now(); const field = data instanceof Error ? 'error' : 'data'; let event = { timestamp, tags, [field]: data, channel }; diff --git a/lib/types/server/options.d.ts b/lib/types/server/options.d.ts index 68408152a..ddf65302f 100644 --- a/lib/types/server/options.d.ts +++ b/lib/types/server/options.d.ts @@ -51,6 +51,13 @@ export interface ServerOptions { */ autoListen?: boolean | undefined; + /** + * @default false. + * Used to control the log timestamp format. When false, shows the timestamp in millisceonds since 1970-01-01. When true, shows the timestamp in ISO-8601 format using UTC timezone. + * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serveroptionsisotimestamp) + */ + isoTimestamp?: boolean | undefined; + /** * @default { engine: require('@hapi/catbox-memory' }. * Sets up server-side caching providers. Every server includes a default cache for storing application state. By default, a simple memory-based cache is created which has limited capacity and