简体   繁体   English

简单的日志scala

[英]Simple logging scala

I implemented a simple 我实现了一个简单的

trait Logging {
   ...
   object log {
      def info(msg: String)
      ...
   }

   //saves stuff 
   def logOut(path) = {}

setting up a full log4j takes a ton of time with all the properties file required 设置完整的log4j与所需的所有属性文件需要花费大量时间

Logging seems to be working fine except for the fact that I need to make a logOut at the end of my main() to print or save those logs somewhere, which is inconvenient and ugly 日志记录似乎工作正常,除了以下事实:我需要在main()的末尾进行注销以在某些地方打印或保存这些日志,这不方便且丑陋

So the questions are : 所以问题是:

  1. Is there a way to force Logging to do something at the end of the program? 有没有办法强制日志记录在程序结束时执行某些操作?
  2. Is there another good way to approach the problem? 还有另一种解决问题的好方法吗?

You can add Shutdown hook at the end of your program. 您可以在程序末尾添加Shutdown hook。 But keep in mind that it will only be executed in case of graceful shutdown. 但是请记住,只有在正常关机的情况下才会执行它。 Killing the process will not invoke this hook. 终止进程将不会调用此挂钩。

sys.addShutdownHook { println("Bye-bye") }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM