简体   繁体   English

Crystal中的全局日志级别

[英]Global log level in Crystal

I'm writing an application in the Crystal programming language. 我正在用Crystal编程语言编写应用程序。 One of the things I like about Crystal is its extensive standard library, which includes things like a Logger utility. 我喜欢Crystal的一个原因是其广泛的标准库,其中包括Logger实用程序之类的东西。

The logging syntax looks like this: 日志记录语法如下所示:

require "logger"

log = Logger.new(STDOUT)
log.level = Logger::WARN

log.debug("Created logger")
log.info("Program started")
log.warn("Nothing to do!")

( Taken from Crystal documentation ) 取自Crystal文档

The problem I'm running into is that it is hard to keep track of severity levels. 我遇到的问题是很难跟踪严重性级别。 Either I have to pass the same Logger object to whatever class/method I want logging in, or I have to pass the severity enum. 要么我必须将相同的Logger对象传递给我想要登录的任何类/方法,要么我必须传递严重性枚举。

How should I deal with this issue? 我该如何处理这个问题? Is there an accepted solution in the Crystal community? Crystal社区中是否有可接受的解决方案?

You could place the logger on a class, for example 例如,您可以将记录器放在类上

module MyApp
  class_getter logger = Logger.new.tap { |l| l.level = Logger::WARN }
end

Then use it with MyApp.logger.info("foo") . 然后将它与MyApp.logger.info("foo")

However this has a few drawbacks: 然而,这有一些缺点:

  1. The logging level is global, you can't edit it per-class. 日志记录级别是全局的,您无法按类编辑它。
  2. Logging within libraries is impossible (they can't see MyApp ) 在库中登录是不可能的(他们看不到MyApp

Maybe it would be worth opening an issue on the Crystal repo to discuss this. 也许值得在Crystal repo上打开一个问题来讨论这个问题。

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

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