Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed js logging not including params #40

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions .zetch.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion js/bitbazaar/log/log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
}
Expand Down Expand Up @@ -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({
Expand Down
37 changes: 20 additions & 17 deletions js/bitbazaar/log/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -194,8 +198,7 @@ class GlobalLog {
if (emitOltp) {
this.logger.emit({
severityText,
body: message,
attributes,
body: this._joined_msg_and_attrs(message, attrs),
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion opencollector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading