tinylog – Estrutura de log leve e simplificada para Java. Tutorial HelloWorld e detalhes do nível de registro
Publicados: 2021-11-09
O que é tinylog?
Tinylog é uma estrutura de log leve e simplificada para muitas linguagens JVM. Possui registrador estático. Não há necessidade de criar qualquer outra instância do registrador por classe.
Como qualquer outra estrutura de registro padrão (log4j), também suporta 5 níveis de registro diferentes.
- VESTÍGIO
- DEPURAR
- INFORMAÇÕES - Padrão
- AVISO
- ERRO
Tenha seu nível de log para trace
para habilitar todos os logs.
O que todos os frameworks que o tinylog suporta:
- Java
- Kotlin
- Escala
- outra linguagem JVM
Quais são as principais vantagens da estrutura de log do tinylog?
- É leve (~ 170kb lib)
- É mais rápido
- É muito simples de implementar.
- Ele segue os padrões log4j padrão
- É de código aberto
Vamos começar a codificar e amostrar Exemplo HelloWorld
Passo 1
- Adicione dependências do tinylog maven ao arquivo pom.xml do seu projeto.

Abra o arquivo pom.xml do seu projeto de produção e adicione abaixo duas dependências.
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 > |
Reconstrua o projeto.
Passo 2
- Adicione tinylog.properties na pasta /resources.
- Arquivo: 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 |
Aqui temos dois adaptadores tinylog.
- adaptador de
console
tinylog- Isso imprime o log no console Eclipse / IntelliJ IDEA
- adaptador de
file
tinylog- Isso imprime o log no arquivo crunchifyLog.txt

Etapa 3
- Criar 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" ) ; } } |
Copie o código acima e salve o arquivo.
Passo 4
Execute o programa acima como aplicativo Java e você verá o resultado abaixo.
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 |
Quais são todos os formatos de log que você pode usar com o tinylog?

Nome | Descrição |
---|---|
CLASSE | Nome de classe totalmente qualificado em que a solicitação de registro é emitida |
NOME DA CLASSE | Nome da classe (sem pacote) onde a solicitação de registro é emitida |
CONTEXTO | Todos os valores definidos do contexto de registro (novo no tinylog 1.1) |
ENCONTRO: DATA | Data e hora da solicitação de registro |
EXCEÇÃO | Exceção lançada ( null , se nenhuma foi lançada) |
ARQUIVO | Nome do arquivo de origem Java de onde a solicitação de registro é emitida |
NÍVEL | Nível de log da entrada de log criada |
LINHA | Número da linha de onde a solicitação de registro é emitida |
MENSAGEM | Mensagem associada da entrada de log criada |
MÉTODO | Nome do método de onde a solicitação de registro é emitida |
PACOTE | Pacote em que a solicitação de registro é emitida |
PROCESS_ID | ID do processo do aplicativo |
RENDERED_LOG_ENTRY | Entrada de log renderizada final como seria usada para saídas baseadas em texto |
THREAD_ID | ID do tópico atual |
THREAD_NAME | Nome do tópico atual |
Quantos escritores você poderia usar com o tinylog?
escritor | Nome | Descrição |
---|---|---|
ConsoleWriter | console | Grava entradas de log no console |
FileWriter | Arquivo | Grava entradas de log em um arquivo definido |
JdbcWriterGenericName | jdbc | Armazena entradas de log em um banco de dados SQL |
LogcatWriter | logcat | Encaminha entradas de log para o sistema de log nativo do Android |
RollingFileWriter | arquivo rolante | Como o FileWriter , mas usa vários arquivos girando-os |
SharedFileWriter | arquivo compartilhado | Suporta a gravação de várias instâncias de um programa no mesmo arquivo |
nulo | nulo | Descarta todas as entradas de log |
Log sem arquivo tinylog.properties
Para referência, aqui está um log se você não tiver nenhuma estrutura de log habilitada para seu aplicativo.

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 |
E você está tudo pronto. Deixe-me saber se você enfrentar algum problema ao executar este tutorial do tinylog.