tinylog – 用于 Java 的轻量级、简化的日志记录框架。 HelloWorld 教程和日志记录级别详细信息
已发表: 2021-11-09
什么是微型日志?
Tinylog 是一个轻量级和简化的日志框架,适用于许多 JVM 语言。 它有静态记录器。 无需为每个类创建任何其他记录器实例。
与任何其他标准日志记录框架 (log4j) 一样,它也支持 5 种不同的日志记录级别。
- 痕迹
- 调试
- 信息 - 默认
- 警告
- 错误
trace
您的日志记录级别以启用所有日志记录。
所有框架 tinylog 支持的内容:
- 爪哇
- 科特林
- 斯卡拉
- 其他 JVM 语言
tinylog 日志框架的主要优点是什么?
- 它是轻量级的(~170kb 库)
- 它更快
- 实现起来非常简单。
- 它遵循标准的 log4j 模式
- 它是开源的
让我们开始编码和示例 HelloWorld 示例
第1步
- 将 tinylog maven 依赖项添加到项目的 pom.xml 文件中。

打开生产项目的 pom.xml 文件并添加以下两个依赖项。
1 2 3 4 5 6 7 8 9 10 |
< dependency > < groupId > org . tinylog < / groupId > < artifactId > tinylog - api < / artifactId > < version > 2.3.2 < / version > < / dependency > < dependency > < groupId > org . tinylog < / groupId > < artifactId > tinylog - impl < / artifactId > < version > 2.3.2 < / version > < / dependency > |
重建项目。
第2步
- 在 /resources 文件夹下添加 tinylog.properties。
- 文件:tinylog.properties

1 2 3 4 5 6 7 8 9 10 |
# logs to Console writerCrunchifyConsole = console writerCrunchifyConsole . format = { date : HH : mm : ss . SSS } { level } : { message } writerCrunchifyConsole . level = trace # logs to File writerCrunchifyFile = file writerCrunchifyFile . file = crunchifyLog . txt writerCrunchifyFile . level = trace |
这里我们有两个 tinylog 适配器。
- tinylog
console
适配器- 这会在 Eclipse / IntelliJ IDEA 控制台中打印日志
- tinylog
file
适配器- 这将在 crunchifyLog.txt 文件中打印日志

第三步
- 创建类 CrunchifyTinyLogFirstTutorial.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package crunchify . com . java . tutorials ; import org . tinylog . Logger ; /** * @author Crunchify.com * Program: tinylog Hello World Tutorial with log levels and properties * */ public class CrunchifyTinyLogFirstTutorial { public static void main ( String [ ] args ) { // Logs a message at INFO level. Logger . info ( "Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - INFO Log Level" ) ; // Logs a message at TRACE level. Logger . trace ( "Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - TRACE Log Level!" ) ; // Logs a message at DEBUG level. Logger . debug ( "Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - DEBUG Log Level!" ) ; // Logs a message at WARN level. Logger . warn ( "Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - WARN Log Level!" ) ; // Logs a message at ERROR level. Logger . error ( "Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - ERROR Log Level!" ) ; String crunchifyString = "Cruncher" ; Logger . info ( "Howdy {}! This is Tinylog tutorial by {}." , crunchifyString , "Crunchify.com" ) ; Logger . error ( "Oh.. This is not {} but it's {}" , "Meta.com" , "Crunchify.com" ) ; } } |
复制上面的代码并保存文件。
第四步
将上述程序作为 Java 应用程序运行,您将看到如下结果。
1 2 3 4 5 6 7 8 9 10 11 |
/ Users / app / Library / Java / JavaVirtualMachines / openjdk - 17.0.1 / Contents / Home / bin / java - javaagent : / Applications / IntelliJ IDEA . app / Contents / lib / idea_rt . jar = 60075 : / Applications / crunchify . com . java . tutorials . CrunchifyTinyLogFirstTutorial 13 : 21 : 52.998 INFO : Howdy Cruncher ! This is App Shah and welcome to Tinylog Tutorial ! - INFO Log Level 13 : 21 : 53.001 TRACE : Howdy Cruncher ! This is App Shah and welcome to Tinylog Tutorial ! - TRACE Log Level ! 13 : 21 : 53.002 DEBUG : Howdy Cruncher ! This is App Shah and welcome to Tinylog Tutorial ! - DEBUG Log Level ! 13 : 21 : 53.002 INFO : Howdy Cruncher ! This is Tinylog tutorial by Crunchify . com . 13 : 21 : 53.002 WARN : Howdy Cruncher ! This is App Shah and welcome to Tinylog Tutorial ! - WARN Log Level ! 13 : 21 : 53.002 ERROR : Howdy Cruncher ! This is App Shah and welcome to Tinylog Tutorial ! - ERROR Log Level ! 13 : 21 : 53.002 ERROR : Oh . . This is not Meta . com but it ' s Crunchify . com Process finished with exit code 0 |
您可以在 tinylog 中使用哪些日志记录格式?

姓名 | 描述 |
---|---|
班级 | 发出日志记录请求的全限定类名 |
班级名称 | 发出记录请求的类名(无包) |
语境 | 来自日志上下文的所有设置值(tinylog 1.1 中的新功能) |
日期 | 记录请求的日期和时间 |
例外 | 抛出异常( null ,如果没有抛出) |
文件 | 发出记录请求的 Java 源文件的文件名 |
等级 | 创建的日志条目的日志级别 |
线 | 发出记录请求的行号 |
信息 | 创建的日志条目的关联消息 |
方法 | 发出记录请求的方法名称 |
包裹 | 发出日志请求的包 |
PROCESS_ID | 应用程序的进程 ID |
RENDERED_LOG_ENTRY | 最终呈现的日志条目,因为它将用于基于文本的输出 |
THREAD_ID | 当前线程的ID |
THREAD_NAME | 当前线程的名称 |
tinylog 可以使用多少个 writer?
作家 | 姓名 | 描述 |
---|---|---|
控制台编写器 | 安慰 | 将日志条目写入控制台 |
文件编写器 | 文件 | 将日志条目写入定义的文件 |
JdbcWriter | 数据库 | 在 SQL 数据库中存储日志条目 |
LogcatWriter | 日志猫 | 将日志条目转发到 Android 的本机日志系统 |
滚动文件编写器 | 滚动文件 | 与FileWriter类似,但通过旋转它们来使用多个文件 |
SharedFileWriter | 共享文件 | 支持将程序的多个实例写入同一个文件 |
空值 | 空值 | 丢弃所有日志条目 |
没有 tinylog.properties 文件的日志
作为参考,如果您没有为您的应用程序启用任何日志记录框架,这里是一个日志。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
< meta charset = "utf-8" / > / Users / app / Library / Java / JavaVirtualMachines / openjdk - 17.0.1 / Contents / Home / bin / java - javaagent : / Applications / IntelliJ IDEA . app / Contents / lib / idea_rt . jar = 60075 : / Applications / crunchify . com . java . tutorials . CrunchifyTinyLogFirstTutorial 2021 - 11 - 08 13 : 28 : 30 [ main ] crunchify . com . java . tutorials . CrunchifyTinyLogFirstTutorial . main ( ) INFO : Howdy Cruncher ! This is App Shah and welcome to Tinylog Tutorial ! - INFO Log Level 2021 - 11 - 08 13 : 28 : 30 [ main ] crunchify . com . java . tutorials . CrunchifyTinyLogFirstTutorial . main ( ) TRACE : Howdy Cruncher ! This is App Shah and welcome to Tinylog Tutorial ! - TRACE Log Level ! 2021 - 11 - 08 13 : 28 : 30 [ main ] crunchify . com . java . tutorials . CrunchifyTinyLogFirstTutorial . main ( ) DEBUG : Howdy Cruncher ! This is App Shah and welcome to Tinylog Tutorial ! - DEBUG Log Level ! 2021 - 11 - 08 13 : 28 : 30 [ main ] crunchify . com . java . tutorials . CrunchifyTinyLogFirstTutorial . main ( ) INFO : Howdy Cruncher ! This is Tinylog tutorial by Crunchify . com . 2021 - 11 - 08 13 : 28 : 30 [ main ] crunchify . com . java . tutorials . CrunchifyTinyLogFirstTutorial . main ( ) WARN : Howdy Cruncher ! This is App Shah and welcome to Tinylog Tutorial ! - WARN Log Level ! 2021 - 11 - 08 13 : 28 : 30 [ main ] crunchify . com . java . tutorials . CrunchifyTinyLogFirstTutorial . main ( ) ERROR : Howdy Cruncher ! This is App Shah and welcome to Tinylog Tutorial ! - ERROR Log Level ! 2021 - 11 - 08 13 : 28 : 30 [ main ] crunchify . com . java . tutorials . CrunchifyTinyLogFirstTutorial . main ( ) ERROR : Oh . . This is not Meta . com but it ' s Crunchify . com Process finished with exit code 0 |
你都准备好了。 如果您在运行此 tinylog 教程时遇到任何问题,请告诉我。