tinylog –Java用の軽量で簡素化されたロギングフレームワーク。 HelloWorldチュートリアルとログレベルの詳細
公開: 2021-11-09
tinylogとは何ですか?
Tinylogは、非常に多くのJVM言語向けの軽量で簡素化されたロギングフレームワークです。 静的ロガーがあります。 クラスごとに他のロガーインスタンスを作成する必要はありません。
他の標準ログフレームワーク(log4j)と同様に、5つの異なるログレベルもサポートします。
- 痕跡
- デバッグ
- INFO –デフォルト
- 警告
- エラー
すべてのログを有効にするには、ログレベルをtrace
します。
tinylogがサポートするすべてのフレームワーク:
- Java
- Kotlin
- Scala
- 他のJVM言語
tinylogロギングフレームワークの主な利点は何ですか?
- 軽量です(〜170kb lib)
- より速い
- 実装は非常に簡単です。
- 標準のlog4jパターンに従います
- オープンソースです
コーディングを始めて、HelloWorldの例のサンプルを見てみましょう
ステップ1
- tinylogmavenの依存関係をプロジェクトのpom.xmlファイルに追加します。

本番プロジェクトのpom.xmlファイルを開き、以下の2つの依存関係を追加します。
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 |
ここに2つのtinylogアダプターがあります。
- tinylog
console
アダプタ- これにより、Eclipse / IntelliJIDEAコンソールにログが出力されます
- tinylog
file
アダプタ- これにより、crunchifyLog.txtファイルにログインが出力されます

ステップ-3
- クラス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" ) ; } } |
上記のコードをコピーしてファイルを保存します。
ステップ-4
上記のプログラムを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で使用できるライターは何人ですか?
作家 | 名前 | 説明 |
---|---|---|
ConsoleWriter | コンソール | ログエントリをコンソールに書き込みます |
FileWriter | ファイル | 定義されたファイルにログエントリを書き込みます |
JdbcWriter | jdbc | ログエントリをSQLデータベースに保存します |
LogcatWriter | logcat | ログエントリをAndroidのネイティブログシステムに転送します |
RollingFileWriter | ローリングファイル | FileWriterと似ていますが、複数のファイルを回転させて使用します |
SharedFileWriter | sharedfile | プログラムの複数のインスタンスを同じファイルに書き込むことをサポートします |
ヌル | ヌル | すべてのログエントリを破棄します |
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チュートリアルの実行で問題が発生した場合は、お知らせください。