From 91621737a316fb43d4af296553857602d323f4a8 Mon Sep 17 00:00:00 2001 From: Zak Stucke Date: Thu, 30 May 2024 17:43:15 +0300 Subject: [PATCH] Fixed js logging not including params --- .zetch.lock | 27 +++++++++++++------------- js/bitbazaar/log/log.test.ts | 25 +++++++++++++++++++++++- js/bitbazaar/log/log.ts | 37 +++++++++++++++++++----------------- opencollector.yaml | 2 +- 4 files changed, 59 insertions(+), 32 deletions(-) diff --git a/.zetch.lock b/.zetch.lock index 343b5c8d..21df2323 100644 --- a/.zetch.lock +++ b/.zetch.lock @@ -1,23 +1,24 @@ { "version": "0.0.10", "files": { - "py_rust/LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", - "js/LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", - "rust/LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", + "docs/CONTRIBUTING.zetch.md": "bace46dc064746b54cf472eba960d934d705c2f83120b865a4b47032ff1552c5", + "py/README.zetch.md": "3657698b60bed2e0971c0792e39585a83e3567ea79e1a890ff390ce09dc1e118", + "README.zetch.md": "4e2cce04c5726d8c1517970fc89d3f2b09e904fdcf7340d03f1941afba826ac3", "CODE_OF_CONDUCT.zetch.md": "bf106326ffc75f5167cfde27c997c77c6b97c843a9e392b564355d0e70e50b97", "py_rust/README.zetch.md": "3657698b60bed2e0971c0792e39585a83e3567ea79e1a890ff390ce09dc1e118", - "docs/CODE_OF_CONDUCT.zetch.md": "bf106326ffc75f5167cfde27c997c77c6b97c843a9e392b564355d0e70e50b97", - "docs/index.zetch.md": "3657698b60bed2e0971c0792e39585a83e3567ea79e1a890ff390ce09dc1e118", - "docs/LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", "js/tsconfig.zetch.json": "fb5d57b825bb3c2f6dd4254bf939f2444e52946622a7f93b91e3acb75876ebbc", - "js/README.zetch.md": "3657698b60bed2e0971c0792e39585a83e3567ea79e1a890ff390ce09dc1e118", - "README.zetch.md": "4e2cce04c5726d8c1517970fc89d3f2b09e904fdcf7340d03f1941afba826ac3", - "docs/CONTRIBUTING.zetch.md": "bace46dc064746b54cf472eba960d934d705c2f83120b865a4b47032ff1552c5", "CONTRIBUTING.zetch.md": "bace46dc064746b54cf472eba960d934d705c2f83120b865a4b47032ff1552c5", - "py/README.zetch.md": "3657698b60bed2e0971c0792e39585a83e3567ea79e1a890ff390ce09dc1e118", - "py/LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", + "docs/index.zetch.md": "3657698b60bed2e0971c0792e39585a83e3567ea79e1a890ff390ce09dc1e118", + "rust/LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", "LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", - "opencollector.yaml.zetch": "678a691ae64d7f9893e8799ea657842fe051b3fcce4da568969d8de070a29393", - "rust/README.zetch.md": "3657698b60bed2e0971c0792e39585a83e3567ea79e1a890ff390ce09dc1e118" + "js/README.zetch.md": "3657698b60bed2e0971c0792e39585a83e3567ea79e1a890ff390ce09dc1e118", + "rust/README.zetch.md": "3657698b60bed2e0971c0792e39585a83e3567ea79e1a890ff390ce09dc1e118", + "rust/pkg/LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", + "py/LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", + "opencollector.yaml.zetch": "33aa6a1e9e386b9e01c3d4c010b70260654c06850fb1433afb15545bd85f2f2d", + "docs/CODE_OF_CONDUCT.zetch.md": "bf106326ffc75f5167cfde27c997c77c6b97c843a9e392b564355d0e70e50b97", + "js/LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", + "py_rust/LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", + "docs/LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b" } } \ No newline at end of file diff --git a/js/bitbazaar/log/log.test.ts b/js/bitbazaar/log/log.test.ts index 13f89636..b4981131 100644 --- a/js/bitbazaar/log/log.test.ts +++ b/js/bitbazaar/log/log.test.ts @@ -17,7 +17,7 @@ describe("Logging/Tracing", () => { console: level_from_or_null ? { level_from: level_from_or_null as LogLevel, - custom_out: (message, ...extra) => { + custom_out: (message) => { logs.push(message); }, } @@ -49,6 +49,29 @@ describe("Logging/Tracing", () => { expect(logs).toEqual(expected_enabled); }, ); + it("Logs: multiple non string args should work", async () => { + const logs: string[] = []; + new GlobalLog({ + console: { + level_from: "DEBUG", + custom_out: (message) => { + logs.push(message); + }, + }, + otlp: { + endpoint: "http://localhost:4318", + level_from: "INFO", + service_name: "js-test", + service_version: "1.0.0", + }, + }); + + LOG.debug("DEBUG", 1, 2, 3); + LOG.info("INFO", 1, 2, 3); + LOG.warn("WARN", 1, 2, 3); + LOG.error("ERROR", 1, 2, 3); + expect(logs).toEqual(["DEBUG 1 2 3", "INFO 1 2 3", "WARN 1 2 3", "ERROR 1 2 3"]); + }); it("Logs: oltp", async () => { // Just confirm nothing errors, when trying to flush and measure output from tests etc seems to cause problems with bun test. new GlobalLog({ diff --git a/js/bitbazaar/log/log.ts b/js/bitbazaar/log/log.ts index 8f869c24..b236c9de 100644 --- a/js/bitbazaar/log/log.ts +++ b/js/bitbazaar/log/log.ts @@ -18,7 +18,7 @@ import { registerInstrumentations } from "@opentelemetry/instrumentation"; import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base"; -import type { LogAttributes, Logger } from "@opentelemetry/api-logs"; +import type { Logger } from "@opentelemetry/api-logs"; import { BatchLogRecordProcessor, LoggerProvider } from "@opentelemetry/sdk-logs"; import type { Meter, MeterOptions } from "@opentelemetry/api"; @@ -35,7 +35,7 @@ export type LogLevel = "DEBUG" | "INFO" | "WARN" | "ERROR"; interface ConsoleArgs { level_from: LogLevel; - custom_out?: (message: string, ...optionalParams: any[]) => void; + custom_out?: (message: string) => void; } interface OltpArgs { @@ -112,26 +112,33 @@ class GlobalLog { } /** Log a debug message. */ - debug(message: string, attributes?: LogAttributes) { - this._log_inner("DEBUG", message, attributes); + debug(message: string, ...attrs: any[]) { + this._log_inner("DEBUG", message, ...attrs); } /** Log an info message. */ - info(message: string, attributes?: LogAttributes) { - this._log_inner("INFO", message, attributes); + info(message: string, ...attrs: any[]) { + this._log_inner("INFO", message, ...attrs); } /** Log a warning message. */ - warn(message: string, attributes?: LogAttributes) { - this._log_inner("WARN", message, attributes); + warn(message: string, ...attrs: any[]) { + this._log_inner("WARN", message, ...attrs); } /** Log an error message. */ - error(message: string, attributes?: LogAttributes) { - this._log_inner("ERROR", message, attributes); + error(message: string, ...attrs: any[]) { + this._log_inner("ERROR", message, ...attrs); } - _log_inner(severityText: LogLevel, message: string, attributes: LogAttributes | undefined) { + _joined_msg_and_attrs(message: string, attrs: any[]) { + if (attrs.length === 0) { + return message; + } + return `${message} ${attrs.join(" ")}`; + } + + _log_inner(severityText: LogLevel, message: string, ...attrs: any[]) { // Log to console if enabled: if (this.console) { let emit = false; @@ -161,10 +168,7 @@ class GlobalLog { if (emit) { if (this.console.custom_out) { - emitter = this.console.custom_out; - } - if (attributes !== undefined) { - emitter(message, attributes); + this.console.custom_out(this._joined_msg_and_attrs(message, attrs)); } else { emitter(message); } @@ -194,8 +198,7 @@ class GlobalLog { if (emitOltp) { this.logger.emit({ severityText, - body: message, - attributes, + body: this._joined_msg_and_attrs(message, attrs), }); } } diff --git a/opencollector.yaml b/opencollector.yaml index 2024bff1..7ee2e638 100644 --- a/opencollector.yaml +++ b/opencollector.yaml @@ -32,7 +32,7 @@ exporters: stream-name: default # Writes all opentelemetry logs, traces, metrics to a file, useful for testing: file/debug_file_writing: - path: /home/runner/work/bitbazaar/bitbazaar/logs/otlp_telemetry_out.log + path: /Users/zak/z/code/bitbazaar/logs/otlp_telemetry_out.log rotation: max_megabytes: 10 max_days: 3