Skip to content

Commit

Permalink
fix: output logs dir when development env (#780)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Dec 30, 2020
1 parent 711cf12 commit 557d874
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 31 deletions.
2 changes: 0 additions & 2 deletions packages/core/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export const createMidwayLogger = (
dir: isDevelopmentEnv
? join(framework.getAppDir(), 'logs', framework.getProjectName())
: join(getUserHome(), 'logs', framework.getProjectName()),
disableFile: isDevelopmentEnv,
disableError: isDevelopmentEnv,
level: isDevelopmentEnv ? 'info': 'warn',
};
return createLogger(name, Object.assign({}, loggerOptions, options));
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface LoggerOptions {
consoleLevel?: LoggerLevel;
fileLevel?: LoggerLevel;
fileMaxSize?: string;
fileMaxFiles?: string;
fileMaxFiles?: number | string;
errMaxSize?: string;
errMaxFiles?: string;
}
Expand Down
9 changes: 5 additions & 4 deletions packages/logger/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class MidwayBaseLogger extends EmptyLogger {

constructor(options: LoggerOptions = {}) {
super(options);
this.exitOnError = false;
this.loggerOptions = options;
if (this.loggerOptions.defaultLabel) {
this.labels.push(this.loggerOptions.defaultLabel);
Expand Down Expand Up @@ -79,8 +80,8 @@ export class MidwayBaseLogger extends EmptyLogger {
level: this.loggerOptions.fileLevel || this.loggerOptions.level || 'silly',
createSymlink: this.loggerOptions.disableFileSymlink !== true,
symlinkName: this.loggerOptions.fileLogName,
maxSize: this.loggerOptions.fileMaxSize || '100m',
maxFiles: this.loggerOptions.fileMaxFiles || null,
maxSize: this.loggerOptions.fileMaxSize || '200m',
maxFiles: this.loggerOptions.fileMaxFiles || '31d',
});
}
this.add(this.fileTransport);
Expand All @@ -99,8 +100,8 @@ export class MidwayBaseLogger extends EmptyLogger {
level: 'error',
createSymlink: this.loggerOptions.disableErrorSymlink !== true,
symlinkName: this.loggerOptions.errorLogName,
maxSize: this.loggerOptions.errMaxSize || '100m',
maxFiles: this.loggerOptions.errMaxFiles || null,
maxSize: this.loggerOptions.errMaxSize || '200m',
maxFiles: this.loggerOptions.errMaxFiles || '31d',
});
}
this.add(this.errTransport);
Expand Down
8 changes: 8 additions & 0 deletions packages/logger/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ describe('/test/index.test.ts', () => {
await removeFileOrDir(join(process.cwd(), 'common-error.log'));
const consoleLogger = createConsoleLogger('consoleLogger');
consoleLogger.error('test console error');
const err = new Error('custom error');
err.name = 'MyCustomError';
consoleLogger.error(err);
consoleLogger.error('before:', err);
consoleLogger.info('启动耗时 %d ms', 111);
consoleLogger.info('%j', {a: 1});
consoleLogger.debug('1', '2', '3');
consoleLogger.info('plain error message', 321);

expect(fileExists(join(process.cwd(), 'common-error.log'))).toBeFalsy();
});
Expand Down
74 changes: 54 additions & 20 deletions packages/web/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import { EggLoggers as BaseEggLoggers, EggLogger, Transport } from 'egg-logger';
import { loggers, ILogger } from '@midwayjs/logger';
import { relative, join } from 'path';
import { existsSync, lstatSync, renameSync } from 'fs';
import { relative, join, isAbsolute, dirname, basename } from 'path';
import { existsSync, lstatSync, readFileSync, renameSync, unlinkSync } from 'fs';
import { Application } from 'egg';
import { MidwayProcessTypeEnum } from '@midwayjs/core';

const levelTransform = (level) => {
switch (level) {
case Infinity: // egg logger 的 none 是这个等级
return null;
case 0:
case 'DEBUG':
case 'debug':
return 'debug';
case 1:
case 'INFO':
case 'info':
return 'info';
case 2:
case 'WARN':
case 'warn':
return 'warn';
case 3:
case 'ERROR':
case 'error':
return 'error';
default:
return 'silly';
Expand All @@ -32,6 +42,7 @@ class WinstonTransport extends Transport {
options.transportName,
Object.assign(options, {
disableConsole: true,
disableError: true, // EggJS 的默认转发到错误日志是通过设置重复的 logger 实现的,在这种情况下代理会造成 midway 写入多个 error 日志,默认需要移除掉
level: levelTransform(options.level),
})
);
Expand All @@ -45,22 +56,40 @@ class WinstonTransport extends Transport {
*/
log(level, args, meta) {
const msg = (super.log(level, args, meta) as unknown) as string;
this.transportLogger.log(level.toLowerCase(), msg.replace('\n', ''));
const winstonLevel = levelTransform(level);
if (winstonLevel) {
this.transportLogger.log(winstonLevel, msg.replace('\n', ''));
}
}
}


function isEmptyFile(p: string) {
let content = readFileSync(p, {
encoding: 'utf8'
});
return content === null || content === undefined || content === '';
}

function checkEggLoggerExists(dir, fileName, eggLoggerFiles) {
const file = join(dir, fileName);
const file = isAbsolute(fileName) ? fileName : join(dir, fileName);
if (existsSync(file) && !lstatSync(file).isSymbolicLink()) {
eggLoggerFiles.push(fileName);
// 如果是空文件,则直接删了,否则加入备份队列
if (isEmptyFile(file)) {
unlinkSync(file);
} else {
eggLoggerFiles.push(fileName);
if (!isAbsolute(fileName)) {
eggLoggerFiles.push(join(dir, fileName));
}
}
}
}

class EggLoggers extends BaseEggLoggers {
app: Application;
/**
* @constructor
* @param {Object} config - egg app config
* - logger
* - {String} env - egg app runtime env string, detail please see `app.config.env`
* - {String} type - current process type, `application` or `agent`
Expand All @@ -77,6 +106,8 @@ class EggLoggers extends BaseEggLoggers {
* - {String} eol - end of line char
* - {String} [concentrateError = duplicate] - whether write error logger to common-error.log, `duplicate` / `redirect` / `ignore`
* - customLogger
* @param options
* @param app
*/
constructor(options, app: Application) {
super(options);
Expand Down Expand Up @@ -112,28 +143,31 @@ class EggLoggers extends BaseEggLoggers {

updateTransport(name: string, eggLoggerFiles: string[]) {
const logger = this.get(name) as EggLogger;
const fileLogName = relative(
(logger as any).options.dir,
(logger as any).options.file
);
const eggLogFile = (logger as any).options.file;
let fileLogName;
let logDir;
if (isAbsolute(eggLogFile)) {
logDir = dirname(eggLogFile);
fileLogName = basename(eggLogFile);
} else {
logDir = (logger as any).options.dir;
fileLogName = relative(logDir, eggLogFile);
}

// 关闭日志
logger.get('file').close();

// 备份老 egg 日志
if (
existsSync((logger as any).options.file) &&
eggLoggerFiles.includes(fileLogName)
existsSync(eggLogFile) &&
eggLoggerFiles.includes(eggLogFile)
) {
const oldFileName = (logger as any).options.file;
const timeFormat = [
new Date().getFullYear(),
new Date().getMonth() + 1,
new Date().getDate(),
].join('-');
renameSync(oldFileName, oldFileName + '.' + timeFormat + '_eggjs_bak');
}

// EggJS 的默认转发到错误日志是通过设置重复的 logger 实现的,在这种情况下代理会造成 midway 写入多个 error 日志,默认需要移除掉
if ((logger as any).duplicateLoggers.has('ERROR')) {
(logger as any).duplicateLoggers.delete('ERROR');
renameSync(eggLogFile, eggLogFile + '.' + timeFormat + '_eggjs_bak');
}

if (this.app.getProcessType() === MidwayProcessTypeEnum.AGENT) {
Expand All @@ -146,7 +180,7 @@ class EggLoggers extends BaseEggLoggers {
logger.set(
'file',
new WinstonTransport({
dir: (logger as any).options.dir,
dir: logDir,
fileLogName,
level: (logger as any).options.level,
transportName: name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { join } = require('path');

exports.watcher = {
// watcher 在 prod 中默认没有设置 type,防止框架开发者忘记设置,这里设置一下,避免报错
type: 'development',
Expand All @@ -7,4 +9,11 @@ exports.logger = {
level: 'DEBUG',
};

exports.customLogger = {
middlewareLogger: {
consoleLevel: 'NONE',
file: join(__dirname, '../../logs/middleware.log'),
}
}

exports.keys = 'foo';
14 changes: 10 additions & 4 deletions packages/web/test/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,18 @@ describe('test/logger.test.js', () => {
app.logger.info('just show once');
app.logger.error('this is a test error');

const middlewareLogger = app.getLogger('middlewareLogger');
middlewareLogger.error('xxxxx');

await sleep();

expect(matchContentTimes(join(process.env.EGG_HOME, 'logs/ali-demo/common-error.log'), 'just show once')).toEqual(0)
expect(matchContentTimes(join(process.env.EGG_HOME, 'logs/ali-demo/common-error.log'), 'this is a test error')).toEqual(1)
expect(matchContentTimes(join(process.env.EGG_HOME, 'logs/ali-demo/midway-web.log'), 'just show once')).toEqual(1)
expect(matchContentTimes(join(process.env.EGG_HOME, 'logs/ali-demo/midway-web.log'), 'this is a test error')).toEqual(1)
// 自定义日志,打印一遍 error,会在自定义日志本身,以及 common-error 中出现
expect(matchContentTimes(join(app.getAppDir(), 'logs/middleware.log'), 'xxxxx')).toEqual(1);
expect(matchContentTimes(join(process.env.EGG_HOME, 'logs/ali-demo/common-error.log'), 'xxxxx')).toEqual(1);
expect(matchContentTimes(join(process.env.EGG_HOME, 'logs/ali-demo/common-error.log'), 'just show once')).toEqual(0);
expect(matchContentTimes(join(process.env.EGG_HOME, 'logs/ali-demo/common-error.log'), 'this is a test error')).toEqual(1);
expect(matchContentTimes(join(process.env.EGG_HOME, 'logs/ali-demo/midway-web.log'), 'just show once')).toEqual(1);
expect(matchContentTimes(join(process.env.EGG_HOME, 'logs/ali-demo/midway-web.log'), 'this is a test error')).toEqual(1);

await closeApp(app);
});
Expand Down

0 comments on commit 557d874

Please sign in to comment.