在 Scala 中登录 [英] Logging in Scala

查看:30
本文介绍了在 Scala 中登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Scala 应用程序中进行日志记录的好方法是什么?符合语言哲学的东西,不会使代码混乱,并且维护成本低且不引人注目.这是一个基本要求列表:

What is a good way to do logging in a Scala application? Something that is consistent with the language philosophy, does not clutter the code, and is low-maintenance and unobtrusive. Here's a basic requirement list:

  • 简单
  • 不会使代码混乱.Scala 的优点在于它的简洁性.我不希望我的一半代码记录语句
  • 可以更改日志格式以适合我的其余企业日志和监控软件
  • 支持日志级别(即调试、跟踪、错误)
  • 可以登录到磁盘以及其他目的地(即套接字、控制台等)
  • 最低配置,如果有的话
  • 在容器(即网络服务器)中工作
  • (可选,但很高兴)作为语言的一部分或作为 Maven 工件出现,所以我不必破解我的构建来使用它

我知道我可以使用现有的 Java 日志记录解决方案,但它们至少在上述两个方面失败,即混乱和配置.

I know I can use the existing Java logging solutions, but they fail on at least two of the above, namely clutter and configuration.

感谢您的回复.

推荐答案

slf4j 包装器

Scala 的大多数日志库都是围绕 Java 日志框架(slf4j、log4j 等)的一些包装器,但截至 2015 年 3 月,幸存的日志库都是 slf4j.这些日志库提供了某种log 对象,您可以调用info(...)debug(...) 等对象. 我不是 slf4j 的忠实粉丝,但它现在似乎是主要的日志框架.以下是 SLF4J 的描述:

slf4j wrappers

Most of Scala's logging libraries have been some wrappers around a Java logging framework (slf4j, log4j etc), but as of March 2015, the surviving log libraries are all slf4j. These log libraries provide some sort of log object to which you can call info(...), debug(...), etc. I'm not a big fan of slf4j, but it now seems to be the predominant logging framework. Here's the description of SLF4J:

Java 或 (SLF4J) 的简单日志外观充当各种日志框架的简单外观或抽象,例如java.util.logging、log4j 和 logback,允许最终用户在部署时插入所需的日志框架.

The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks, e.g. java.util.logging, log4j and logback, allowing the end user to plug in the desired logging framework at deployment time.

在部署时更改底层日志库的能力为整个 slf4j 系列记录器带来了独特的特性,您需要注意:

The ability to change underlying log library at deployment time brings in unique characteristic to the entire slf4j family of loggers, which you need to be aware of:

  1. 作为配置的类路径方法.slf4j 知道您正在使用哪个底层日志库的方法是通过某个名称加载一个类.我遇到了在自定义类加载器时 slf4j 无法识别我的记录器的问题.
  2. 因为 simple facade 试图成为公分母,所以它仅限于实际的日志调用.换句话说,无法通过代码进行配置.
  1. classpath as configuration approach. The way slf4j knows which underlying logging library you are using is by loading a class by some name. I've had issues in which slf4j not recognizing my logger when classloader was customized.
  2. Because the simple facade tries to be the common denominator, it's limited only to actual log calls. In other words, the configuration cannot be done via the code.

在一个大型项目中,如果每个人都使用 slf4j,那么能够控制传递依赖的日志记录行为实际上会很方便.

In a large project, it could actually be convenient to be able to control the logging behavior of transitive dependencies if everyone used slf4j.

Scala Logging 由 Heiko Seeberger 编写,作为他的 slf4s.它使用宏将调用扩展为 if 表达式以避免潜在的昂贵的日志调用.

Scala Logging is written by Heiko Seeberger as a successor to his slf4s. It uses macro to expand calls into if expression to avoid potentially expensive log call.

Scala Logging 是一个方便且高性能的日志库,它封装了 SLF4J 和其他潜在的日志库.

Scala Logging is a convenient and performant logging library wrapping logging libraries like SLF4J and potentially others.

历史记录器

  • Logula,由 Coda Hale 编写的 Log4J 包装器.以前喜欢这个,现在弃用了.
  • configgy,一个 java.util.logging 包装器,曾经在 Scala 早期很流行.现已废弃.
  • Historical loggers

    • Logula, a Log4J wrapper written by Coda Hale. Used to like this one, but now it's abandoned.
    • configgy, a java.util.logging wrapper that used to be popular in the earlier days of Scala. Now abandoned.
    • 这篇关于在 Scala 中登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆