博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TinyLog –轻量级Java日志记录框架教程
阅读量:2530 次
发布时间:2019-05-11

本文共 8361 字,大约阅读时间需要 27 分钟。

is a simple and lightweight logging framework for Java. We can use tinylog with Java, Kotlin, Scala, and Android applications.

是Java的简单轻量级日志记录框架。 我们可以将tinylog与Java,Kotlin,Scala和Android应用程序一起使用。

TinyLog日志记录框架的好处 (Benefits of TinyLog Logging Framework)

  1. Avoids boiler-plate code of Logger initialization. The Logger class in tinylog is static and used directly to log messages.

    避免Logger初始化的样板代码。 tinylog中的Logger类是静态的,可直接用于记录消息。
  2. Support for other popular JVM languages such as Kotlin and Scala.

    支持其他流行的JVM语言,例如Kotlin和Scala。
  3. Support for android applications too using logcat.

    也支持使用logcat的android应用程序。
  4. The tinylog jars are lightweight and small in size. The tinylog 2.x version API jar is 48 kb and implementation jar is 72 kb only.

    tinylog罐子重量轻且尺寸小。 tinylog 2.x版本的API jar为48 kb,实现jar为72 kb。
  5. The output can be sent to Console, File, database using JDBC and DataSource.

    可以使用JDBC和DataSource将输出发送到控制台,文件,数据库。
  6. The configuration file is very simple. For simple console based logging, we don’t need any configuration file.

    配置文件非常简单。 对于基于控制台的简单日志记录,我们不需要任何配置文件。
  7. It’s free and open source. The complete project code is hosted on .

    它是免费和开源的。 完整的项目代码托管在 。
  8. According to their , they are much faster than .

    根据他们 ,它们比快得多。
  9. There is no dependency on any other API and framework.

    不依赖于任何其他API和框架。
  10. Support for creating tags to categorize the log messages. It’s very useful in filtering log messages from a huge log file.

    支持创建标签以对日志消息进行分类。 这对于从巨大的日志文件中过滤日志消息非常有用。
  11. Lazy Logging support to defer expensive computation only if the logging is required.

    懒惰日志支持仅在需要日志时才推迟昂贵的计算。

TinyLog日志入门 (Getting Started with the TinyLog Logging)

We have to include following jars into our project classpath. These are the latest versions of the tinylog framework as of writing this tutorial.

我们必须在项目类路径中包含以下jar。 这些是撰写本教程时tinylog框架的最新版本。

  • tinylog-api-2.0.0-RC1.jar

    tinylog-api-2.0.0-RC1.jar
  • tinylog-impl-2.0.0-RC1.jar

    tinylog-impl-2.0.0-RC1.jar

Most of the time, we use Maven or Gradle for our project build and dependencies management.

大多数时候,我们使用Maven或Gradle进行项目构建和依赖管理。

Minelog的Maven依赖关系 (Maven Dependencies for tinylog)

org.tinylog
tinylog-api
2.0.0-RC1
org.tinylog
tinylog-impl
2.0.0-RC1

tinylog的Gradle依赖关系 (Gradle Dependencies for tinylog)

implementation 'org.tinylog:tinylog-api:2.0.0-RC1'implementation 'org.tinylog:tinylog-impl:2.0.0-RC1'
The above implementation jar is for JVM based applications. There are separate implementation jars for Kotlin, Scala, Android, SLF4J, and JCL. You have to include the implementation JAR based on the type of your project.
上面的实现jar适用于基于JVM的应用程序。 对于Kotlin,Scala,Android,SLF4J和JCL,有单独的实现jar。 您必须根据您的项目类型包括实施JAR。

使用tinylog记录器 (Using tinylog Logger)

Once the required jars are added to the project classpath, we can use its Logger class to write logging messages.

将所需的jar添加到项目类路径后,我们可以使用其Logger类编写日志消息。

Below image shows my Eclipse project structure.

下图显示了我的Eclipse项目结构。

Tinylog Example Project

TinyLog Example Project

TinyLog示例项目

Here are the classes where I am using tinylog Logger for logging.

这是我使用tinylog Logger进行记录的类。

Employee.java: A simple model class with some properties and their .

Employee.java :一个简单的模型类,具有一些属性及其 。

package com.journaldev.tinylog;import org.tinylog.Logger;public class Employee {	private int id;	private String name;	public String getName() {		Logger.debug("Returning employee name " + this.name);		return name;	}	public void setName(String name) {		Logger.info("Setting employee name " + name);		this.name = name;	}	public int getId() {		Logger.debug("Returning employee id " + this.id);		return id;	}	public void setId(int id) {		Logger.info("Setting employee id " + id);		this.id = id;	}}

EmployeeService.java: A simple service class that provides a method to create an Employee object.

EmployeeService.java :一个简单的服务类,提供了一种创建Employee对象的方法。

package com.journaldev.tinylog;import org.tinylog.Logger;public class EmployeeService {	public Employee createEmployee(int i, String n) {		Logger.info("Creating Employee with ID = " + i + " and Name = " + n);		Employee emp = new Employee();		emp.setId(i);		emp.setName(n);		return emp;	}	}

EmployeeMain.java: The main class to run our simple project.

EmployeeMain.java :运行我们的简单项目的主类。

package com.journaldev.tinylog;import org.tinylog.Logger;public class EmployeeMain {	public static void main(String[] args) {				Logger.debug("Program Started");				EmployeeService empService = new EmployeeService();		Employee emp = empService.createEmployee(10, "Pankaj");		System.out.println("Employee ID = " + emp.getId());		System.out.println("Employee Name = " + emp.getName());		Logger.debug("Program Finished");	}}

When we run the above program, it generates the following output. Note that we haven’t created the tinylog configuration file at this stage.

当我们运行上述程序时,它将生成以下输出。 请注意,我们目前尚未创建tinylog配置文件。

Tinylog Logging Example Output Console

TinyLog Logging Example Output Console

TinyLog日志记录示例输出控制台

TinyLog记录级别 (TinyLog Logging Levels)

Logger supports five logging levels – trace, debug, info, warn, and error. The default and the lowest priority level is “trace”. The highest priority level is “error”. We can set the logging level in the configuration file. The log messages of the level set and the higher priority will get logged.

记录器支持五个记录级别-跟踪,调试,信息,警告和错误。 默认值和最低优先级为“跟踪”。 最高优先级是“错误”。 我们可以在配置文件中设置日志记录级别。 设置级别和更高优先级的日志消息将被记录。

TinyLog配置文件 (TinyLog Configuration File)

TinyLog looks for the tinylog.properties file in the classpath to configure logging options. We usually create this file in the resources directory. Let’s create our first tinylog configuration file.

TinyLog在类路径中寻找tinylog.properties文件,以配置日志记录选项。 我们通常在资源目录中创建此文件。 让我们创建第一个tinylog配置文件。

tinylog.properties:

tinylog.properties

writer        = consolewriter.format = {date: HH:mm:ss.SSS} {tag} {level}: {message}

When we run the main program again, the output is:

当我们再次运行主程序时,输出为:

12:22:47.073  DEBUG: Program Started12:22:47.076  INFO: Creating Employee with ID = 10 and Name = Pankaj12:22:47.076  INFO: Setting employee id 1012:22:47.077  INFO: Setting employee name Pankaj12:22:47.077  DEBUG: Returning employee id 10Employee ID = 1012:22:47.077  DEBUG: Returning employee name PankajEmployee Name = Pankaj12:22:47.077  DEBUG: Program Finished

Notice that the log messages are following the format mentioned in the configuration file.

请注意,日志消息遵循配置文件中提到的格式。

TinyLog标签 (TinyLog Tags)

We can set Logger tag in the code using below code.

我们可以使用以下代码在代码中设置Logger标签。

Logger.tag("MAIN").debug("Program Started");Logger.tag("MAIN").debug("Program Finished");

The updated log messages will contain the following lines.

更新后的日志消息将包含以下几行。

12:26:12.831 MAIN DEBUG: Program Started...12:26:12.835 MAIN DEBUG: Program Finished

We can set a writer tag to log messages having that tag, the other messages will be ignored by the writer.

我们可以设置一个writer标签来记录具有该标签的消息,其他消息将被writer忽略。

writer     = consolewriter.tag = SYSTEM

We can ignore all the tags and only log untagged messages by setting the tag as “-“.

通过将标签设置为“-”,我们可以忽略所有标签,仅记录未加标签的消息。

writer     = consolewriter.tag = -

TinyLog配置,可将消息记录到日志文件中 (TinyLog Configuration to log messages into a log file)

Here is a simple example to log messages into tinylog.txt file.

这是一个将消息记录到tinylog.txt文件中的简单示例。

writer       =  filewriter.file  = tinylog.txtwriter.level = infowriter.format = {date: HH:mm:ss.SSS} {tag} {level}: {message}

TinyLog supports reading environment variables too. For example, we can specify the log file to be generated at home directory using the following format.

TinyLog也支持读取环境变量。 例如,我们可以使用以下格式指定要在主目录中生成的日志文件。

writer.file = ${HOME}/tinylog.txt

We can also read system properties in the TinyLog configuration using the format #{key}.

我们还可以使用#{key}格式在TinyLog配置中读取系统属性。

writer.format = #{user.name}: {message}

滚动文件编写器配置 (Rolling File Writer Configuration)

writer       = rolling filewriter.file  = tinylog{count}.txtwriter.level = infowriter.format = {date: HH:mm:ss.SSS} {tag} {level}: {message}

扩展TingLog (Extending TingLog)

TinyLog is extendable. We can implement org.tinylog.writers.Writer interface and register it as a service. Similarly, we can implement org.tinylog.policies.Policy interface to create custom policies and register it. You can get more details about it .

TinyLog是可扩展的。 我们可以实现org.tinylog.writers.Writer接口并将其注册为服务。 同样,我们可以实现org.tinylog.policies.Policy接口来创建自定义策略并进行注册。 您可以获取有关它的更多详细信息。

. 签出最终项目。

翻译自:

转载地址:http://wcqzd.baihongyu.com/

你可能感兴趣的文章
thinkphp3.2.3入口文件详解
查看>>
POJ 1141 Brackets Sequence
查看>>
Ubuntu 18.04 root 使用ssh密钥远程登陆
查看>>
Servlet和JSP的异同。
查看>>
虚拟机centOs Linux与Windows之间的文件传输
查看>>
ethereum(以太坊)(二)--合约中属性和行为的访问权限
查看>>
IOS内存管理
查看>>
middle
查看>>
[Bzoj1009][HNOI2008]GT考试(动态规划)
查看>>
Blob(二进制)、byte[]、long、date之间的类型转换
查看>>
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>