Skip to content
Draft
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
10 changes: 7 additions & 3 deletions packages/collector/src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ exports.init = function init(userConfig = {}) {
parentLogger = uninstrumentedLogger({
name: '@instana/collector',
level: 'info',
base: { threadId, pid: process.pid, hostname: os.hostname() }
base: { threadId, pid: process.pid, hostname: os.hostname() },
// Set ISO timestamp format for the default logger
timestamp: () => `,"time":"${new Date().toISOString()}"`
});
}

Expand All @@ -121,7 +123,8 @@ exports.init = function init(userConfig = {}) {
const pinoOpts = {
...parentLogger.levels,
level: parentLogger.level || 'info',
base: parentLogger.bindings()
base: parentLogger.bindings(),
timestamp: () => `,"time":"${new Date().toISOString()}"`
};

// CASE: Either its the customers pino instance (and probably a different pino installation/version)
Expand All @@ -132,7 +135,8 @@ exports.init = function init(userConfig = {}) {
const symbols = Object.getOwnPropertySymbols(instance);

// CASE: We copy any settings from the pino instance such as custom formats.
if (symbols && Array.isArray(symbols)) {
if (userConfig.logger && symbols && Array.isArray(symbols)) {
// If there's a custom timestamp function from the userConfig logger , we'll respect that
const timeSym = symbols.find(sym => String(sym) === 'Symbol(pino.time)');
// @ts-ignore
const timestampFn = instance[timeSym];
Expand Down
23 changes: 23 additions & 0 deletions packages/collector/test/logger_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,29 @@ describe('logger', () => {
stream => expect(stream.level).to.equal('info')
);
});

it('should use ISO timestamp format for pino logger', () => {
const logger = log.init({});

// Create a mock write stream to capture the log output
const capturedLogs = [];
const mockStream = {
write: function (chunk) {
capturedLogs.push(JSON.parse(chunk));
}
};

const originalStream = logger.logger[uninstrumentedLogger.symbols.streamSym];
logger.logger[uninstrumentedLogger.symbols.streamSym] = mockStream;

logger.info('Test log message');

logger.logger[uninstrumentedLogger.symbols.streamSym] = originalStream;

expect(capturedLogs.length).to.equal(1);
expect(capturedLogs[0]).to.have.property('time');
expect(capturedLogs[0].time).to.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/);
});
});

/**
Expand Down