百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

aliyun-log-logback-appender

> 编程语言
开源

Logback Appender 中文版README 阿里云日志 Logback Appender Logback 旨在取代流行的 log4j 项目。您可以控制日志的输出方式和输出位置。

200 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Logback Appender 中文版README 阿里云日志 Logback Appender Logback 旨在取代流行的 log4j 项目。您可以控制日志的输出方式和输出位置。

Logback Appender

中文版README

Aliyun Log Logback Appender

Logback is intended as a successor to the popular log4j project. You can control the destination of the log through logback. It can be console, file, GUI components, socket, NT event log, syslog. You can control the output format for each log as well. You can control the generation process of the log through log level. The most interesting thing is you can complete the above things through a configuration file and without any code modification.

You can set the destination of your log to AliCloud Log Service through Aliyun Log Logback Appender, The format of the log in AliCloud Log Service is as follows:

level: ERROR
location: com.aliyun.openservices.log.logback.example.LogbackAppenderExample.main(LogbackAppenderExample.java:18)
message: error log
throwable: java.lang.RuntimeException: xxx
thread: main
time: 2018-01-02T03:15+0000
log: 2018-01-02 11:15:29,682 ERROR [main] com.aliyun.openservices.log.logback.example.LogbackAppenderExample: error log
__source__: xxx
__topic__: yyy

Field Specifications:

  • level stands for log level
  • location is logs's output position, can be disabled by configuration
  • message is the content of the log
  • throwable is exception of the log (this field will appear only if the exception is recorded)
  • thread stands for thread name
  • time is the log's generation time (you can configure it's format through timeFormat and timeZone)
  • log is custom log format (this field will appear only if you configure the encoder)
  • __source__ is the log's source, you can specify its value in conf file
  • __topic__ is the log's topic, you can specify its value in conf file

Advantage

  • Disk Free: the generation data will be send to AliCloud Log Service in real time through network.
  • Without Refactor: if your application already use logback, you can just add logback appender to your configuration file.
  • Asynchronous and High Throughput: the data will be send to AliCloud Log Service asynchronously. It is suitable for high concurrent write.
  • Context Query: at server side, in addition to searching log with keywords, you can obtain the context information of original log as well.

Supported Version

  • logback 1.2.3
  • aliyun-log-producer 0.3.8
  • protobuf-java 2.5.0

Configuration Steps

1. Adding the Dependencies in pom.xml


    com.google.protobuf
    protobuf-java
    2.5.0

    com.aliyun.openservices
    aliyun-log-logback-appender
    0.1.28

2. Modify the Configuration File

Take logback.xml as an example, you can configure the appender and logger related to AliCloud Log Services as follows:

  
  

  
    
    
    your project endpoint
    your accesskey id
    your accesskey

    
    your project
    your logStore
    

    
    your topic
    your source

    
    104857600
    0
    8
    524288
    4096
    2000
    10
    100
    50000
    
    
    
        %d %-5level [%thread] %logger{0}: %msg
    
    
    
    yyyy-MM-dd'T'HH:mmZ
    
    UTC
    
    true
    
    true
    
    
    
            ${accessKeyId}
            ${accessKeySecret}
    
    
    500
    
    s
  

  

Note:

  • To prevent data loss when the process exits, please remember to add label DelayingShutdownHook.
  • The LoghubAppender will catch the exceptions in the process of running and put them into BasicStatusManager, you can obtain the exception information through OnConsoleStatusListener or other means. Reference: https://logback.qos.ch/access.html

Parameter Description

The Aliyun Log Logback Appender provides following parameters.

…

Custom CredentialsProvider

The logback-appender supports custom CredentialsProvider. By implementing the CredentialsProvider interface, you can implement advanced features yourself, such as dynamic rotation of AccessKey.

Once a CredentialsProvider is provided, it's unnecessary to configure parameter for static credentials (AccessKeyId/AccessKeySecret/SecurityToken); the system will use the credentials fetched through the CredentialsProvider provided.

  1. Defines a class, eg. MyCredentialsProvider, and implements the CredentialsProvider interface to support the dynamic updates of credentials. Make sure the implementation is thread-safe.

    • For performance, it is recommended that method getCredentials caches credentials and refreshes them before expired.
    java
    class MyCredentialsProvider implements CredentialsProvider {
        @Override
        public synchronized Credentials getCredentials() {
            // fetch credentials and caches credentials
        }
        // constructor
        MyCredentialsProvider(String param1, long paramField2) {}
    }
  2. Defines a class, eg. MyBuilder, and implements the CredentialsProviderBuilder interface.

    • The method getCredentialsProvider should return a new instance of CredentialsProvider for each call.
    java
    class MyBuilder implements CredentialsProviderBuilder {
      @Override
      public CredentialsProvider getCredentialsProvider() {
         return new MyCredentialsProvider(param1, paramField2);
      }
      private String param1;
      private long paramField2;
      public void setParam1(String param1) {
          this.param1 = param1;
      }
      public void setParamField2(long paramField2) {
          this.paramField2 = paramField2;
      }
    }
  3. Configure logback with xml file, set the class of credentialsProviderBuilder with the fully qualified name of your class, eg. com.example.MyBuilder, and pass any custom parameters.

    xml
      
        
            hello
            123
         
        
      

Custom parameter

To pass in some custom parameters to the MyBuilder class, such as param1, paramField2, define the setter methods setParam1 and setParamField2 in the class first.

java
class MyBuilder implements CredentialsProviderBuilder {
    private String param1;
    private long paramField2;
    public void setParam1(String param1) {
        this.param1 = param1;
    }
    public void setParamField2(long paramField2) {
        this.paramField2 = paramField2;
    }
    // Omit other codes
}

Then configure logback with xml file, set the class of credentialsProviderBuilder with the fully qualified name of your class, eg. com.example.MyBuilder, and fill with custom parameters.

xml

    
        hello
        123
    

Sample Code

LogbackAppenderExample.java

logback-example.xml

Contributors

@lionbule @zzboy made a great contribution to this project.

Thanks for the excellent work by @lionbule @zzboy.

Issues· 43 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Java

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言