Skip to content

Commit

Permalink
%context --> %logger to match log4j pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed Feb 12, 2020
1 parent 94a89b8 commit bf3215a
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/core/server/logging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ There are two types of layout supported at the moment: `pattern` and `json`.

With `pattern` layout it's possible to define a string pattern with special placeholders wrapped into curly braces that
will be replaced with data from the actual log message. By default the following pattern is used:
`[%date][%level][%context%]%meta {message}`. Also `highlight` option can be enabled for `pattern` layout so that
`[%date][%level][%logger]%meta {message}`. Also `highlight` option can be enabled for `pattern` layout so that
some parts of the log message are highlighted with different colors that may be quite handy if log messages are forwarded
to the terminal with color support.

Expand Down
2 changes: 1 addition & 1 deletion src/core/server/logging/integration_tests/logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function createRoot() {
layout: {
highlight: false,
kind: 'pattern',
pattern: '%level|%context|%message',
pattern: '%level|%logger|%message',
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/logging/layouts/conversions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
export { Conversion } from './type';

export { ContextConversion } from './context';
export { LoggerConversion } from './logger';
export { LevelConversion } from './level';
export { MessageConversion } from './message';
export { MetaConversion } from './meta';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import chalk from 'chalk';
import { Conversion } from './type';
import { LogRecord } from '../../log_record';

export const ContextConversion: Conversion = {
pattern: /%context/g,
export const LoggerConversion: Conversion = {
pattern: /%logger/g,
convert(record: LogRecord, highlight: boolean) {
let message = record.context;
if (highlight) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/logging/layouts/layouts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('`create()` creates correct layout.', () => {
const patternLayout = Layouts.create({
highlight: false,
kind: 'pattern',
pattern: '[%date][%level][%context] $message',
pattern: '[%date][%level][%logger] %message',
});
expect(patternLayout).toBeInstanceOf(PatternLayout);

Expand Down
28 changes: 14 additions & 14 deletions src/core/server/logging/layouts/pattern_layout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ test('`format()` correctly formats record with full pattern.', () => {
});

test('`format()` correctly formats record with custom pattern.', () => {
const layout = new PatternLayout('mock-%message-%context-%message');
const layout = new PatternLayout('mock-%message-%logger-%message');

for (const record of records) {
expect(layout.format(record)).toMatchSnapshot();
Expand Down Expand Up @@ -167,15 +167,15 @@ test('`format()` correctly formats record with highlighting.', () => {
});

test('allows specifying the PID in custom pattern', () => {
const layout = new PatternLayout('%pid-%context-%message');
const layout = new PatternLayout('%pid-%logger-%message');

for (const record of records) {
expect(layout.format(record)).toMatchSnapshot();
}
});

test('`format()` allows specifying pattern with meta.', () => {
const layout = new PatternLayout('%context-%meta-%message');
const layout = new PatternLayout('%logger-%meta-%message');
const record = {
context: 'context',
level: LogLevel.Debug,
Expand Down Expand Up @@ -207,70 +207,70 @@ describe('format', () => {

describe('supports specifying a predefined format', () => {
it('ISO8601', () => {
const layout = new PatternLayout('[%date{ISO8601}][%context]');
const layout = new PatternLayout('[%date{ISO8601}][%logger]');

expect(layout.format(record)).toBe('[2012-02-01T14:30:22.011Z][context]');
});

it('ISO8601_TZ', () => {
const layout = new PatternLayout('[%date{ISO8601_TZ}][%context]');
const layout = new PatternLayout('[%date{ISO8601_TZ}][%logger]');

expect(layout.format(record)).toBe('[2012-02-01T09:30:22.011-05:00][context]');
});

it('ABSOLUTE', () => {
const layout = new PatternLayout('[%date{ABSOLUTE}][%context]');
const layout = new PatternLayout('[%date{ABSOLUTE}][%logger]');

expect(layout.format(record)).toBe('[09:30:22.011][context]');
});

it('UNIX', () => {
const layout = new PatternLayout('[%date{UNIX}][%context]');
const layout = new PatternLayout('[%date{UNIX}][%logger]');

expect(layout.format(record)).toBe('[1328106622][context]');
});

it('UNIX_MILLIS', () => {
const layout = new PatternLayout('[%date{UNIX_MILLIS}][%context]');
const layout = new PatternLayout('[%date{UNIX_MILLIS}][%logger]');

expect(layout.format(record)).toBe('[1328106622011][context]');
});
});

describe('supports specifying a predefined format and timezone', () => {
it('ISO8601', () => {
const layout = new PatternLayout('[%date{ISO8601}{America/Los_Angeles}][%context]');
const layout = new PatternLayout('[%date{ISO8601}{America/Los_Angeles}][%logger]');

expect(layout.format(record)).toBe('[2012-02-01T14:30:22.011Z][context]');
});

it('ISO8601_TZ', () => {
const layout = new PatternLayout('[%date{ISO8601_TZ}{America/Los_Angeles}][%context]');
const layout = new PatternLayout('[%date{ISO8601_TZ}{America/Los_Angeles}][%logger]');

expect(layout.format(record)).toBe('[2012-02-01T06:30:22.011-08:00][context]');
});

it('ABSOLUTE', () => {
const layout = new PatternLayout('[%date{ABSOLUTE}{America/Los_Angeles}][%context]');
const layout = new PatternLayout('[%date{ABSOLUTE}{America/Los_Angeles}][%logger]');

expect(layout.format(record)).toBe('[06:30:22.011][context]');
});

it('UNIX', () => {
const layout = new PatternLayout('[%date{UNIX}{America/Los_Angeles}][%context]');
const layout = new PatternLayout('[%date{UNIX}{America/Los_Angeles}][%logger]');

expect(layout.format(record)).toBe('[1328106622][context]');
});

it('UNIX_MILLIS', () => {
const layout = new PatternLayout('[%date{UNIX_MILLIS}{America/Los_Angeles}][%context]');
const layout = new PatternLayout('[%date{UNIX_MILLIS}{America/Los_Angeles}][%logger]');

expect(layout.format(record)).toBe('[1328106622011][context]');
});
});
it('formats several conversions patterns correctly', () => {
const layout = new PatternLayout(
'[%date{ABSOLUTE}{America/Los_Angeles}][%context][%date{UNIX}]'
'[%date{ABSOLUTE}{America/Los_Angeles}][%logger][%date{UNIX}]'
);

expect(layout.format(record)).toBe('[06:30:22.011][context][1328106622]');
Expand Down
6 changes: 3 additions & 3 deletions src/core/server/logging/layouts/pattern_layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { LogRecord } from '../log_record';
import { Layout } from './layouts';
import {
Conversion,
ContextConversion,
LoggerConversion,
LevelConversion,
MetaConversion,
MessageConversion,
Expand All @@ -34,7 +34,7 @@ import {
/**
* Default pattern used by PatternLayout if it's not overridden in the configuration.
*/
const DEFAULT_PATTERN = `[%date][%level][%context]%meta %message`;
const DEFAULT_PATTERN = `[%date][%level][%logger]%meta %message`;

export const patternSchema = schema.string({
validate: string => {
Expand All @@ -49,7 +49,7 @@ const patternLayoutSchema = schema.object({
});

const conversions: Conversion[] = [
ContextConversion,
LoggerConversion,
MessageConversion,
LevelConversion,
MetaConversion,
Expand Down

0 comments on commit bf3215a

Please sign in to comment.