Maven 日志记录

Maven 站点中提供了最终用户日志记录文档。本文档侧重于内部实现细节。

日志记录 API

Maven 使用Plexus Container logging API,就像任何其他 Plexus 组件一样,即LoggerManager / Logger

从 Maven 3.1.0 开始:

日志实现

Maven 3.1.0 与SLF4J 简单记录器捆绑在一起,但准备好使用其他日志记录实现:SLF4J 负责加载实现,称为“SLF4J 绑定”

日志配置加载实际上是通过日志实现完成的,没有任何 Maven 扩展来支持将 Maven 安装配置与每个用户的配置合并,例如:`$ maven.home /conf/logging` 目录被添加到核心的类路径中(请参阅`$ maven.主页/bin/m2.conf`)。有关文件名、格式等的详细信息,请参阅您的实施文档。

During Maven initialization, Maven tweaks default root logging level to match CLI verbosity choice. Since such feature isn't available in SLF4J API, logging implementation specific extensions need to be added into Maven to support these CLI options: see Slf4jConfigurationFactory / Slf4jConfiguration.

Getting Logger Instance

Plexus Logger and LoggerManager can be injected in Plexus component using Plexus annotations

import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;

@Component( role = MyComponent.class )
public class DefaultMyComponent
    implements MyComponent
{
    @Requirement
    private Logger logger;

    @Requirement
    private LoggerManager loggerManager;
}

Starting with Maven 3.1.0, SLF4J Logger can be used directly too, without Plexus. Of course, this will only work when run under Maven 3.1.0, then this technique can be used safely only in Maven core components or in plugins/component not requiring compatibility with previous Maven versions.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyClass
{
   final Logger logger = LoggerFactory.getLogger( MyClass.class );
}

Logger Name

Before Maven 3.1.0, with logging implementation done in Maven, logger name wasn't used by basic console logging implementation: they are as-is, without clear convention on when to pass logger from class to class or when to create a new logger.

从 Maven 3.1.0 开始,如果记录器名称定义明确,记录实现可能会发挥最大作用。这个定义仍然需要定义和实现:

  • 经典的“类名”模式?
  • 特定于 Maven 的名称层次结构?
  • 混合(一些具有类名,一些具有特定于 Maven 的层次结构)?