本文共 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应用程序一起使用。
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框架的最新版本。
Most of the time, we use Maven or Gradle for our project build and dependencies management.
大多数时候,我们使用Maven或Gradle进行项目构建和依赖管理。
org.tinylog tinylog-api 2.0.0-RC1 org.tinylog tinylog-impl 2.0.0-RC1
implementation 'org.tinylog:tinylog-api:2.0.0-RC1'implementation 'org.tinylog:tinylog-impl:2.0.0-RC1'
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项目结构。
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配置文件。
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 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.
请注意,日志消息遵循配置文件中提到的格式。
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 = -
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}
writer = rolling filewriter.file = tinylog{count}.txtwriter.level = infowriter.format = {date: HH:mm:ss.SSS} {tag} {level}: {message}
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/