Merge pull request #328 from joschi/JENKINS-53551

[JENKINS-53551] Map log levels sent to Sentry
This commit is contained in:
Baptiste Mathus 2018-10-21 11:31:09 +02:00 committed by GitHub
commit ed22e19215
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 1 deletions

View File

@ -22,6 +22,37 @@ class Sentry {
this.raven.config(sentryUrl);
}
/**
* Map java.util.logging log levels to Sentry.io log levels.
* If the level is missing or invalid, the fallback is "info".
* @param {string} level
* @see {@link https://docs.oracle.com/javase/8/docs/api/java/util/logging/Level.html|java.util.logging.Level}
* @see {@link https://docs.sentry.io/clients/node/usage/#raven-node-additional-data|Sentry.io - Raven - additional data}
*/
mapJavaLogLevel(level) {
if (!level) {
logger.error('Missing log level.');
return 'info';
}
switch (level.toUpperCase()) {
case 'SEVERE':
return 'error';
case 'WARNING':
return 'warning';
case 'INFO':
case 'CONFIG':
return 'info';
case 'FINE':
case 'FINER':
case 'FINEST':
return 'debug';
default:
logger.warn(`Unknown log level "${level}", using "info"`);
return 'info';
}
}
/**
* Send the JSON output to Sentry.io. JSON format is from the Error Telemetry API.
* @param {json} data
@ -33,7 +64,7 @@ class Sentry {
}
const errorData = {
level: data.log.level.toLowerCase(),
level: this.mapJavaLogLevel(data.log.level),
logger: data.log.name,
user: {
/*

View File

@ -8,4 +8,16 @@ describe('Sentry lib', () => {
it('does nothing with empty data', () => {
this.sentry.sendOutput(null);
});
it('maps JUL levels correctly', () => {
expect(this.sentry.mapJavaLogLevel(null)).toBe('info');
expect(this.sentry.mapJavaLogLevel('severe')).toBe('error');
expect(this.sentry.mapJavaLogLevel('warning')).toBe('warning');
expect(this.sentry.mapJavaLogLevel('config')).toBe('info');
expect(this.sentry.mapJavaLogLevel('info')).toBe('info');
expect(this.sentry.mapJavaLogLevel('fine')).toBe('debug');
expect(this.sentry.mapJavaLogLevel('finer')).toBe('debug');
expect(this.sentry.mapJavaLogLevel('finest')).toBe('debug');
expect(this.sentry.mapJavaLogLevel('foobar')).toBe('info');
});
});