Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mendess committed Nov 28, 2024
1 parent 9d3931b commit 7f7d8d7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export default {
// API options here:
testEnvironmentOptions: {
bindings: {
LOGGING_SHIM_TOKEN: '',
ENVIRONMENT: 'tests',
WSHIM_TOKEN: 'token',
WSHIM_ENDPOINT: 'https://example.com',
VERSION_METADATA: '',
},
wranglerConfigEnv: 'production',
Expand Down
17 changes: 14 additions & 3 deletions src/context/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class WshimLogger {
private logs: LogEntry[] = [];
private serviceToken: string;
private sampleRate: number;
private fetcher: typeof fetch;
private fetcher?: typeof fetch;
private loggingEndpoint: string;

constructor(env: Bindings, sampleRate: number = 1) {
Expand All @@ -185,7 +185,7 @@ export class WshimLogger {

this.serviceToken = env.WSHIM_TOKEN;
this.sampleRate = sampleRate;
this.fetcher = env.WSHIM_SOCKET?.fetch ?? fetch;
this.fetcher = env.ENVIRONMENT === 'tests' ? undefined : env.WSHIM_SOCKET?.fetch ?? fetch;
this.loggingEndpoint = `${env.WSHIM_ENDPOINT}/log`;
}

Expand All @@ -196,15 +196,24 @@ export class WshimLogger {
log(...msg: unknown[]): void {
if (!this.shouldLog()) return;

if (this.fetcher === undefined) {
console.log(...msg);
return;
}

const message = msg.map(o => (typeof o === 'object' ? JSON.stringify(o) : String(o))).join(' ');
const logEntry: LogEntry = { message, level: 'info' };

this.logs.push(logEntry);
}

error(...msg: unknown[]): void {
if (!this.shouldLog()) return;

if (this.fetcher === undefined) {
console.log(...msg);
return;
}

let logEntry: LogEntry;

if (msg.length === 1 && msg[0] instanceof Error) {
Expand All @@ -231,6 +240,8 @@ export class WshimLogger {
logs: this.logs,
});

if (this.fetcher === undefined) return;

try {
const response = await this.fetcher(this.loggingEndpoint, {
method: 'POST',
Expand Down

0 comments on commit 7f7d8d7

Please sign in to comment.