dtzk85937 2018-10-08 23:55
浏览 693

如何将日志写入多个文件?

In Golang how you guys manage to write logs into multiple file base on the package name.

For example in my current app, I am trying to collect multiple hardware stats from different packages called Netapp, IBM etc but under the same application. So, I would like to write logs from those package in separate folder like /var/log/myapp/netapp.log and /var/log/myapp/ibm.log?

Any pointer or clue would be very helpful ?

Thanks James

  • 写回答

1条回答 默认 最新

  • duanjiwu0324 2018-10-09 08:48
    关注

    One approach you could take is to implement the Observer pattern. It's a great approach when you need to make several things happen with the same input/event. In your case, logging the same input to different logs. You can find more information here.

    In a situation you described and following this example, you can do following things:

    1. Your different logging implementations (with different logging destination folders) can implement the Observer interface by putting your logging code for each logging implementation in OnNotify method.
    2. Create an instance of eventNotifier and register all your logging implementations with eventNotifier.Register method. Something like:

        notifier := eventNotifier{
           observers: map[Observer]struct{}{},
        }
      
        notifier.Register(netAppLogger)
      
        notifier.Register(ibmLogger)
      
    3. Use eventNotifier.Notify whenever and wherever you need to do logging and it will use all registered logging implementations.

    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛