Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

agent logger的初始化在configWillLoad之前进行了 #3548

Closed
sinalvee opened this issue Mar 16, 2019 · 2 comments
Closed

agent logger的初始化在configWillLoad之前进行了 #3548

sinalvee opened this issue Mar 16, 2019 · 2 comments
Assignees

Comments

@sinalvee
Copy link
Contributor

What happens?

agent logger的初始化在configWillLoad之前进行了,导致无法在agent.jsconfigWillLoad钩子更改日志的配置

最小可复现仓库

请使用 egg-init --type=simple bug 创建,并上传到你的 GitHub 仓库

https://github.com/sinalvee/eggjs-agent-logger-init-issue

复现步骤,错误日志以及相关配置

  1. config/config.default.js中设置日志目录
  config.logger = {
    dir: path.join(appInfo.root, 'logs/configDir'),
  };
  1. agent.js configWillLoad钩子修改日志目录
const path = require('path');

class AgentBootHook {
  constructor(agent) {
    this.agent = agent;
  }

  configWillLoad() {
    this.agent.config.logger.dir = path.join(this.agent.baseDir, 'logs/agentDir');
  }
}


module.exports = AgentBootHook;
  1. app.js configWillLoad钩子修改日志目录
const path = require('path');

class AppBootHook {
  constructor(app) {
    this.app = app;
  }

  configWillLoad() {
    this.app.config.logger.dir = path.join(this.app.baseDir, 'logs/appDir');
  }
}

module.exports = AppBootHook;
  1. 启动服务,app的日志被写到了logs/appDir中,而agent的日志目录并没有被更改

问题分析

egg源码中,egg/lib/agent.js文件_wrapMessenger方法中使用了this.coreLogger.warn,因此loggerconfigWillLoad之前就初始化了

  _wrapMessenger() {
    for (const methodName of [ 'broadcast', 'sendTo', 'sendToApp', 'sendToAgent', 'sendRandom' ]) {
      wrapMethod(methodName, this.messenger, this.coreLogger);
    }

    function wrapMethod(methodName, messenger, logger) {
      const originMethod = messenger[methodName];
      messenger[methodName] = function() {
        const stack = new Error().stack.split('\n').slice(1).join('\n');
        logger.warn('agent can\'t call %s before server started\n%s',
          methodName, stack);
        originMethod.apply(this, arguments);
      };
      messenger.once('egg-ready', () => {
        messenger[methodName] = originMethod;
      });
    }
  }

相关环境信息

  • 操作系统:mac
  • Node 版本:v10.13.0
  • Egg 版本:2.15.1
@atian25
Copy link
Member

atian25 commented Mar 18, 2019

logger 的配置,直接在 config 里面修改就好了,没必要到 boot 这里改

@sinalvee
Copy link
Contributor Author

我们这边日志目录的规则是/data/logs/版本号-项目名-端口,并行的版本多了的话,就需要把日志的配置都复制粘贴一遍到新的conig.{env}.js里,想在boot这里设置是想省掉一些复制粘贴的操作,config.{env}.js里只需要修改版本信息,日志目录在一个地方处理就好

killagu added a commit that referenced this issue Apr 1, 2019
@killagu killagu mentioned this issue Apr 1, 2019
3 tasks
killagu added a commit that referenced this issue Apr 4, 2019
killagu added a commit that referenced this issue Apr 13, 2019
popomore pushed a commit that referenced this issue Apr 16, 2019
feat: add BaseHookClass (#3581)

Refs: #3454
Fixes: #3548
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants