Improve Loki logs render with ANSI colors (#15558)

* Improve Loki logs render with ANSI colors

* fixup! Improve Loki logs render with ANSI colors

* fixup! Improve Loki logs render with ANSI colors

* fixup! Improve Loki logs render with ANSI colors
This commit is contained in:
Valentin Agachi
2019-02-22 20:13:10 +08:00
committed by David
parent c4c150bd15
commit 425636ff70
5 changed files with 66 additions and 17 deletions

View File

@@ -11,11 +11,11 @@ import {
describe('getLoglevel()', () => {
it('returns no log level on empty line', () => {
expect(getLogLevel('')).toBe(LogLevel.unkown);
expect(getLogLevel('')).toBe(LogLevel.unknown);
});
it('returns no log level on when level is part of a word', () => {
expect(getLogLevel('this is information')).toBe(LogLevel.unkown);
expect(getLogLevel('this is information')).toBe(LogLevel.unknown);
});
it('returns same log level for long and short version', () => {
@@ -158,4 +158,46 @@ describe('mergeStreamsToLogs()', () => {
},
]);
});
it('detects ANSI codes', () => {
expect(
mergeStreamsToLogs([
{
labels: '{foo="bar"}',
entries: [
{
line: "foo: 'bar'",
ts: '1970-01-01T00:00:00Z',
},
],
},
{
labels: '{bar="foo"}',
entries: [
{
line: "bar: 'foo'",
ts: '1970-01-01T00:00:00Z',
},
],
},
]).rows
).toMatchObject([
{
entry: "bar: 'foo'",
hasAnsi: false,
key: 'EK1970-01-01T00:00:00Z{bar="foo"}',
labels: { bar: 'foo' },
logLevel: 'unknown',
raw: "bar: 'foo'",
},
{
entry: "foo: 'bar'",
hasAnsi: true,
key: 'EK1970-01-01T00:00:00Z{foo="bar"}',
labels: { foo: 'bar' },
logLevel: 'unknown',
raw: "foo: 'bar'",
},
]);
});
});

View File

@@ -1,3 +1,4 @@
import ansicolor from 'ansicolor';
import _ from 'lodash';
import moment from 'moment';
@@ -11,6 +12,7 @@ import {
LogsStreamLabels,
LogsMetaKind,
} from 'app/core/logs_model';
import { hasAnsiCodes } from 'app/core/utils/text';
import { DEFAULT_MAX_LINES } from './datasource';
/**
@@ -21,7 +23,7 @@ import { DEFAULT_MAX_LINES } from './datasource';
*/
export function getLogLevel(line: string): LogLevel {
if (!line) {
return LogLevel.unkown;
return LogLevel.unknown;
}
let level: LogLevel;
Object.keys(LogLevel).forEach(key => {
@@ -33,7 +35,7 @@ export function getLogLevel(line: string): LogLevel {
}
});
if (!level) {
level = LogLevel.unkown;
level = LogLevel.unknown;
}
return level;
}
@@ -125,6 +127,7 @@ export function processEntry(
const timeFromNow = time.fromNow();
const timeLocal = time.format('YYYY-MM-DD HH:mm:ss');
const logLevel = getLogLevel(line);
const hasAnsi = hasAnsiCodes(line);
return {
key,
@@ -133,7 +136,9 @@ export function processEntry(
timeEpochMs,
timeLocal,
uniqueLabels,
entry: line,
hasAnsi,
entry: hasAnsi ? ansicolor.strip(line) : line,
raw: line,
labels: parsedLabels,
searchWords: search ? [search] : [],
timestamp: ts,