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 教程時遇到任何問題,請告訴我。