tinylog – Cadre de journalisation léger et simplifié pour Java. Tutoriel HelloWorld et détails du niveau de journalisation
Publié: 2021-11-09
Qu'est-ce que TinyLog ?
Tinylog est un framework de journalisation léger et simplifié pour de nombreux langages JVM. Il a un enregistreur statique. Pas besoin de créer une autre instance de logger par classe.
Comme tout autre framework de journalisation standard (log4j), il prend également en charge 5 niveaux de journalisation différents.
- TRACE
- DÉBOGUER
- INFORMATIONS – Par défaut
- ATTENTION
- ERREUR
Ayez votre niveau de journalisation à trace
pour activer toute la journalisation.
Ce que tous les frameworks tinylog supportent :
- Java
- Kotlin
- Scala
- autre langage JVM
Quels sont les principaux avantages du framework de journalisation Tinylog ?
- C'est léger (~170kb lib)
- C'est plus rapide
- C'est très simple à mettre en oeuvre.
- Il suit les modèles log4j standard
- C'est opensource
Commençons le codage et échantillonnons l'exemple HelloWorld
Étape 1
- Ajoutez des dépendances tinylog maven au fichier pom.xml de votre projet.

Ouvrez le fichier pom.xml de votre projet de production et ajoutez ci-dessous deux dépendances.
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 > |
Reconstruisez le projet.
Étape 2
- Ajoutez tinylog.properties sous le dossier /resources.
- Fichier : 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 |
Ici, nous avons deux adaptateurs Tinylog.
- Adaptateur de
console
Tinylog- Cela imprime le journal dans la console Eclipse / IntelliJ IDEA
- adaptateur
file
tinylog- Cela imprime le journal dans le fichier crunchifyLog.txt

Étape 3
- Créer la classe 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" ) ; } } |
Copiez le code ci-dessus et enregistrez le fichier.
Étape 4
Exécutez le programme ci-dessus en tant qu'application Java et vous verrez le résultat ci-dessous.
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 |
Quels sont tous les formats de journalisation que vous pouvez utiliser avec tinylog ?

Nom | La description |
---|---|
CLASSER | Nom de classe complet où la demande de journalisation est émise |
NOM DU COURS | Nom de la classe (sans package) où la demande de journalisation est émise |
LE CONTEXTE | Toutes les valeurs définies du contexte de journalisation (nouveau dans tinylog 1.1) |
DATE | Date et heure de la demande de journalisation |
EXCEPTION | Exception levée ( null , si aucune n'a été levée) |
FICHIER | Nom de fichier du fichier source Java à partir duquel la demande de journalisation est émise |
NIVEAU | Niveau de journalisation de l'entrée de journal créée |
LIGNE | Numéro de ligne à partir de laquelle la demande de journalisation est émise |
UN MESSAGE | Message associé à l'entrée de journal créée |
MÉTHODE | Nom de la méthode à partir de laquelle la demande de journalisation est émise |
EMBALLER | Package où la demande de journalisation est émise |
PROCESS_ID | ID de processus de l'application |
RENDERED_LOG_ENTRY | Entrée de journal rendue finale telle qu'elle serait utilisée pour les sorties textuelles |
THREAD_ID | ID du fil en cours |
THREAD_NAME | Nom du fil en cours |
Combien de rédacteurs pourriez-vous utiliser avec Tinylog ?
Écrivain | Nom | La description |
---|---|---|
ConsoleWriter | console | Écrit les entrées du journal dans la console |
FileWriter | fichier | Écrit les entrées du journal dans un fichier défini |
JdbcWriter | jdbc | Stocke les entrées de journal dans une base de données SQL |
LogcatWriter | logcat | Transfère les entrées du journal vers le système de journalisation natif d'Android |
RollingFileWriter | fichier roulant | Comme FileWriter mais utilise plusieurs fichiers en les faisant pivoter |
SharedFileWriter | fichier partagé | Prend en charge l'écriture de plusieurs instances d'un programme dans le même fichier |
nul | nul | Ignore toutes les entrées du journal |
Journal sans fichier tinylog.properties
Pour référence, voici un journal si vous n'avez pas de framework de journalisation activé pour votre application.

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 |
Et vous êtes prêt. Faites-moi savoir si vous rencontrez un problème lors de l'exécution de ce didacticiel sur Tinylog.