Skip to content

Commit

Permalink
fix: logger eol with default value (#1018)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Apr 29, 2021
1 parent 6adc3ed commit 7d3f58d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/logger/src/logger.ts
Expand Up @@ -143,6 +143,7 @@ export class MidwayBaseLogger extends EmptyLogger implements IMidwayLogger {
symlinkName: this.loggerOptions.fileLogName,
maxSize: this.loggerOptions.fileMaxSize || '200m',
maxFiles: this.loggerOptions.fileMaxFiles || '31d',
eol: this.loggerOptions.eol || os.EOL,
});
}
this.add(this.fileTransport);
Expand All @@ -163,6 +164,7 @@ export class MidwayBaseLogger extends EmptyLogger implements IMidwayLogger {
symlinkName: this.loggerOptions.errorLogName,
maxSize: this.loggerOptions.errMaxSize || '200m',
maxFiles: this.loggerOptions.errMaxFiles || '31d',
eol: this.loggerOptions.eol || os.EOL,
});
}
this.add(this.errTransport);
Expand Down
26 changes: 26 additions & 0 deletions packages/logger/test/index.test.ts
Expand Up @@ -26,6 +26,7 @@ import {
} from './util';
import { EggLogger } from 'egg-logger';
import { readFileSync, writeFileSync } from 'fs';
import * as os from 'os';

describe('/test/index.test.ts', () => {
it('should test create logger', async () => {
Expand Down Expand Up @@ -82,6 +83,10 @@ describe('/test/index.test.ts', () => {
expect(includeContent(join(logsDir, 'common-error.log'), 'hello world1')).toBeFalsy();
expect(includeContent(join(logsDir, 'common-error.log'), 'hello world5')).toBeTruthy();

// test default eol
expect(includeContent(join(logsDir, 'midway-core.log'), os.EOL)).toBeTruthy();
expect(includeContent(join(logsDir, 'common-error.log'), os.EOL)).toBeTruthy();

coreLogger.close();
await removeFileOrDir(logsDir);
});
Expand Down Expand Up @@ -716,4 +721,25 @@ describe('/test/index.test.ts', () => {

});

it('should change eol', async () => {
clearAllLoggers();
const logsDir = join(__dirname, 'logs');
await removeFileOrDir(logsDir);

const logger = createFileLogger('file', {
dir: logsDir,
fileLogName: 'test-logger.log',
eol: 'bbb\n'
});

logger.info('file logger');
logger.info('file logger1');
logger.info('file logger2');
await sleep();

expect(matchContentTimes(join(logsDir, 'test-logger.log'), 'bbb\n')).toEqual(3);

await removeFileOrDir(logsDir);
});

});

0 comments on commit 7d3f58d

Please sign in to comment.