gcp-structured-logger
Advanced tools
Comparing version
@@ -24,3 +24,3 @@ /// <reference types="express-serve-static-core" /> | ||
export interface LogEntry { | ||
timestamp: Date; | ||
timestamp: bigint; | ||
severity: LogSeverity; | ||
@@ -27,0 +27,0 @@ insertId?: string; |
{ | ||
"name": "gcp-structured-logger", | ||
"version": "1.4.6", | ||
"version": "1.4.7", | ||
"description": "Structured logger for GCP logging", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,2 @@ | ||
const { format, formatWithOptions, inspect } = require('util') | ||
const { format, formatWithOptions, inspect, types: { isDate } } = require('util') | ||
const { LogSeverity, CONSOLE_SEVERITY } = require('./severity') | ||
@@ -6,2 +6,3 @@ const cleanupForJSON = require('./cleanup-for-json') | ||
const { requestToErrorReportingHttpRequest } = require('./request-transformers') | ||
const { now: nowNS, hrToTimestamp, timestampToISOString, NS_MICROSECOND, NS_MILLISECOND, NS_SECOND, NS_MINUTE, NS_HOUR } = require('./hr-time') | ||
@@ -30,8 +31,2 @@ /** | ||
const NS_MICROSECOND = 1000n | ||
const NS_MILLISECOND = 1000n * NS_MICROSECOND | ||
const NS_SECOND = 1000n * NS_MILLISECOND | ||
const NS_MINUTE = 60n * NS_SECOND | ||
const NS_HOUR = 60n * NS_MINUTE | ||
/** @typedef {import('express-serve-static-core').Request | import('next/server').NextRequest} Request */ | ||
@@ -130,3 +125,3 @@ | ||
reportError(err, severity) { | ||
const timestamp = new Date() | ||
const timestamp = nowNS() | ||
if (!severity) { | ||
@@ -136,3 +131,3 @@ severity = LogSeverity.ERROR | ||
} | ||
const event = { eventTime: timestamp.toISOString(), ...this._makeReportableError(err) } | ||
const event = { eventTime: timestamp, ...this._makeReportableError(err) } | ||
if (!event.context.user) delete event.context.user | ||
@@ -219,3 +214,3 @@ if (!event.context.user && !event.context.httpRequest) delete event.context | ||
trace(...args) { | ||
const now = new Date() | ||
const now = nowNS() | ||
const trace = { name: args.length === 0 ? 'Trace' : '' } | ||
@@ -327,5 +322,5 @@ Error.captureStackTrace(trace, this.trace) | ||
* @param {any[]} args | ||
* @param {Date} [timestamp] | ||
* @param {bigint} [timestamp] | ||
*/ | ||
_writeFormatted(severity, args, timestamp = new Date()) { | ||
_writeFormatted(severity, args, timestamp = nowNS()) { | ||
let data | ||
@@ -381,6 +376,4 @@ const [message, ...a] = args | ||
if (data && typeof data === 'object' && 'eventTime' in data) { | ||
// ErrorMessage | ||
const eventTime = new Date(data.eventTime) | ||
// Use the error event timestamp instead (as long as its valid) | ||
if (!isNaN(eventTime.getTime())) metadata.timestamp = eventTime | ||
if (typeof data.eventTime === 'bigint') metadata.timestamp = data.eventTime | ||
} | ||
@@ -393,3 +386,2 @@ | ||
// @ts-expect-error: message not returned | ||
const { message, ...messageData } = (() => { | ||
@@ -410,10 +402,4 @@ if (typeof data === 'object' && data) { | ||
const timestampNS = hrToTimestamp(metadata.timestamp) | ||
if (process.env.NODE_ENV === 'production') { | ||
// See https://cloud.google.com/logging/docs/agent/configuration#timestamp-processing | ||
const timestamp = (() => { | ||
const ms = metadata.timestamp.getTime() | ||
const seconds = Math.floor(ms / 1000) | ||
const nanos = (ms - (seconds * 1000)) * 1e6 | ||
return { seconds, nanos } | ||
})() | ||
@@ -434,3 +420,3 @@ if (typeof this._productionTransport === 'function') { | ||
...metadata, | ||
timestamp, | ||
timestamp: timestampNS, | ||
logName: this._logName, | ||
@@ -454,3 +440,3 @@ } | ||
message, | ||
timestamp | ||
timestamp: timestampNS | ||
} | ||
@@ -477,3 +463,3 @@ for (const key in metadata) { | ||
} else { | ||
let prefix = metadata.timestamp.toISOString() | ||
let prefix = timestampToISOString(timestampNS, 3) | ||
if (metadata.trace) prefix += ' / ' + metadata.trace.replace(/.+\//, '') | ||
@@ -565,2 +551,2 @@ /** @type {any[]} */ | ||
module.exports = { StructuredLogger, StructuredRequestLogger } | ||
module.exports = { StructuredLogger, StructuredRequestLogger, nowNS } |
42314
3.21%12
9.09%966
3.87%