douzi1117 2014-05-09 23:55
浏览 94


I'm new to Go and trying to find a solution to the following problem.

  1. The goal is to write all logs to a file.
  2. package main imports package A.
  3. In package main, in the main method, logging is setup to write to a file
  4. package A has an init function. In the init function of A, there is a log line - log.Fatal("I am package A").
  5. Since main package imports A, A's init function is first called (even before we get a chance to setup the logs to write to a file).

How to resolve this so the log "I am package A" gets written to a file?

  • 写回答

2条回答 默认 最新

  • douhuan1979 2014-05-10 00:13

    Move logging initialization out to a log package that is imported by both main and A and does not depend directly or indirectly on the parts of your app that you want to log to the file during initialization. From the descriptions of init order in Effective Go and the spec, that should be enough to make sure your logging is initialized first thing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 主要进行描述非满管状态下,管路的摩阻系数是怎么变化的,在管路长度方向上是怎么分布的(标签-matlab)
  • ¥15 动态多目标优化,求解定义的问题的POF和POS公式
  • ¥30 实验 :UML2.0 行为建模
  • ¥15 设计一种优化调度算法并结合论文中的案例给出调度计划
  • ¥15 py文件打包exe报错TypeError:expected str,bytes or os.PathLike object,not_io.BytesIO
  • ¥15 Java编写学籍信息管理系统
  • ¥15 IPXE网刻客户机找不到服务端
  • ¥15 小贝360-4 配二个 华772S 设置WⅰFi5G 连接
  • ¥15 vs2022的QT报错,好像是缺少winextras
  • ¥15 怎么看 cst中一个面的功率分布图