Skip to content

Commit

Permalink
misc: make info logs more structured
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKolarik committed Dec 15, 2024
1 parent a5e913f commit 7a4d9cd
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 11 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"elastic-apm-utils": "^4.1.1",
"got": "^14.2.1",
"gunzip-maybe": "^1.4.2",
"h-logger2": "^1.2.6",
"h-logger2": "^1.3.1",
"h-logger2-elastic": "^6.2.1",
"http-errors": "^2.0.0",
"ipaddr.js": "^2.2.0",
Expand Down
8 changes: 6 additions & 2 deletions src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ if (process.env['ELASTIC_SEARCH_URL']) {
});
}

const loggerOptions = {
inspectOptions: { breakLength: 120 },
};

const logger = new Logger(
'globalping-api',
esClient ? [
new ConsoleWriter(Number(process.env['LOG_LEVEL']) as LogLevelValue || Logger.levels.info),
new ConsoleWriter(Number(process.env['LOG_LEVEL']) as LogLevelValue || Logger.levels.info, loggerOptions),
new ElasticWriter(Number(process.env['LOG_LEVEL']) as LogLevelValue || Logger.levels.info, { esClient, apmClient: apmAgent }),
] : [
new ConsoleWriter(Number(process.env['LOG_LEVEL']) as LogLevelValue || Logger.levels.trace),
new ConsoleWriter(Number(process.env['LOG_LEVEL']) as LogLevelValue || Logger.levels.trace, loggerOptions),
],
);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/ws/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ io

socket.emit('api:connect:location', location);
socket.emit('api:connect:adoption', { isAdopted });
logger.info(`ws client ${socket.id} connected from ${location.city}, ${location.country} [${probe.ipAddress} - ${location.network}]`);
logger.info(`WS client connected`, { client: { id: socket.id, ip: probe.ipAddress }, location: { city: location.city, country: location.country, network: location.network } });

// Handlers
socket.on('probe:status:update', handleStatusUpdate(probe));
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ws/helper/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const errorHandler = (next: NextArgument) => (socket: ServerSocket, mwNex
const clientIp = getProbeIp(socket) ?? '';
const reason = isError(error) ? error.message : 'unknown';

logger.info(`Disconnecting client ${socket.id} for (${reason}) [${clientIp}]`);
logger.info(`Disconnecting client for (${reason})`, { client: { id: socket.id, ip: clientIp } });
logger.debug('Details:', error);

if (mwNext) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ws/helper/probe-ip-limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class ProbeIpLimit {
const previousProbe = await this.getProbeByIp(ip, { allowStale: false });

if (previousProbe && previousProbe.client !== socketId) {
logger.warn(`ws client ${socketId} has reached the concurrent IP limit.`, { message: previousProbe.ipAddress });
logger.warn(`WS client ${socketId} has reached the concurrent IP limit.`, { message: previousProbe.ipAddress });
throw new ProbeError('ip limit');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ws/helper/subscribe-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const subscribeWithHandler = (socket: ServerSocket, event: string, method
const clientIp = probe.ipAddress;
const reason = isError(error) ? error.message : 'unknown';

logger.info(`Event "${event}" failed to handle. ${socket.id} for (${reason}) [${clientIp}]`);
logger.info(`Event "${event}" failed to handle for (${reason})`, { client: { id: socket.id, ip: clientIp } });
logger.debug(`Details:`, error);
}
});
Expand Down

0 comments on commit 7a4d9cd

Please sign in to comment.