Vous avez assez d'écrire du code de journalisation personnalisé pour vos applications Python?
Logly est un utilitaire de journalisation prêt à aller qui fournit un moyen facile de enregistrer des messages avec différents niveaux, couleurs et de nombreuses options personnalisées. Il est conçu pour être flexible, vous permettant de personnaliser les messages de journal en fonction des besoins de votre application. Logly prend en charge la journalisation à la fois à la console et à un fichier, et il est livré avec des niveaux de journal codés en couleur intégrés pour une meilleure visibilité.
Si vous aimez ce projet, assurez-vous de jouer? dans le référentiel et si vous souhaitez contribuer, assurez-vous de déborder ce référentiel❤.
pip install logly # Import Logly
from logly import Logly
# Create a Logly instance
logly = Logly ()
# logly = Logly(show_time=False) # Include timestamps in log messages default is true, and you can set it to false will not show the time in all log messages
# Start logging will store the log in text file
logly . start_logging () #make sure to include this or else the log will only display without storing it in file
logly . info ( "hello this is log" )
logly . info ( "hello this is log" , color = logly . COLOR . RED ) # with custom color
# Log messages with different levels and colors
logly . info ( "Key1" , "Value1" , color = logly . COLOR . CYAN )
logly . warn ( "Key2" , "Value2" , color = logly . COLOR . YELLOW )
logly . error ( "Key3" , "Value3" , color = logly . COLOR . RED )
logly . debug ( "Key4" , "Value4" , color = logly . COLOR . BLUE )
logly . critical ( "Key5" , "Value5" , color = logly . COLOR . CRITICAL )
logly . fatal ( "Key6" , "Value6" , color = logly . COLOR . CRITICAL )
logly . trace ( "Key7" , "Value7" , color = logly . COLOR . BLUE )
logly . log ( "Key8" , "Value8" , color = logly . COLOR . WHITE )
# Stop logging (messages will be displayed but not logged in file after this point)
logly . stop_logging ()
# Log more messages after stopping logging (messages will be displayed but not logged in file after this point)
logly . info ( "AnotherKey1" , "AnotherValue1" , color = logly . COLOR . CYAN )
logly . warn ( "AnotherKey2" , "AnotherValue2" , color = logly . COLOR . YELLOW )
logly . error ( "AnotherKey3" , "AnotherValue3" , color = logly . COLOR . RED )
logly . info ( "hello this is log" , color = logly . COLOR . RED , show_time = False ) # with custom color and without time
# Start logging again
logly . start_logging ()
# Set the default file path and max file size
logly . set_default_file_path ( "log.txt" ) # Set the default file path is "log.txt" if you want to set the file path where you want to save the log file.
logly . set_default_max_file_size ( 50 ) # set default max file size is 50 MB
# Log messages with default settings (using default file path and max file size)
logly . info ( "DefaultKey1" , "DefaultValue1" )
logly . warn ( "DefaultKey2" , "DefaultValue2" )
logly . error ( "DefaultKey3" , "DefaultValue3" , log_to_file = False )
#The DEFAULT FILE SIZE IS 100 MB in the txt file
# Log messages with custom file path and max file size(optional)
logly . info ( "CustomKey1" , "CustomValue1" , file_path = "path/c.txt" , max_file_size = 25 ) # max_file_size is in MB and create a new file when the file size reaches max_file_size
logly . warn ( "CustomKey2" , "CustomValue2" , file_path = "path/c.txt" , max_file_size = 25 , auto = True ) # auto=True will automatically delete the file data when it reaches max_file_size
# Access color constants directly
logly . info ( "Accessing color directly" , "DirectColorValue" , color = logly . COLOR . RED )
# Disable color
logly . color_enabled = False
logly . info ( "ColorDisabledKey" , "ColorDisabledValue" , color = logly . COLOR . RED )
logly . info ( "ColorDisabledKey1" , "ColorDisabledValue1" , color = logly . COLOR . RED , color_enabled = True ) # This will enable the color for this one log message
logly . color_enabled = True
# this will enable the color again
logly . info ( "ColorDisabledKey1" , "ColorDisabledValue1" , color = logly . COLOR . RED , color_enabled = False ) # this will disable the color for this one log message
# Display logged messages (this will display all the messages logged so far)
print ( "Logged Messages:" )
for message in logly . logged_messages :
print ( message )Logly à partir du module logly .Logly .start_logging() .stop_logging() .Pour plus d'informations, consultez le référentiel
Si vous rencontrez une erreur liée au chemin de fichier par défaut, vous pouvez utiliser l'extrait de code suivant pour définir le chemin par défaut:
import os
from logly import Logly
logly = Logly ()
logly . start_logging ()
# Set the default file path and maximum file size
logly . set_default_max_file_size ( 50 )
logger = os . path . join ( os . path . dirname ( os . path . abspath ( __file__ )), "log.txt" )
logly . set_default_file_path ( logger )Cela définira le chemin du fichier par défaut et vous pouvez le personnaliser en fonction de vos exigences.
Si vous souhaitez définir le chemin par défaut du fichier journal, vous pouvez utiliser l'extrait de code suivant
from logly import Logly
logly = Logly ()
logly . set_default_file_path ( "log.txt" ) Si vous avez fait face à une erreur comme FileNotFoundError: [Errno 2] No such file or directory: 'log.txt' Vous pouvez utiliser l'extrait de code suivant pour définir le chemin par défaut
import os
from logly import Logly
logly = Logly () # initialize the logly
logly . start_logging () # make sure to include this or else the log will only display without storing it
logly . set_default_max_file_size ( 50 ) # optional
logger = os . path . join ( os . path . dirname ( os . path . abspath ( __file__ )), "log.txt" ) # This will ensure the path location to create the log.txt on current directory
logly . set_default_file_path ( logger )Pour plus d'informations, consultez le référentiel.
| Niveau | Code couleur |
|---|---|
| Informations | Cyan |
| AVERTISSEMENT | JAUNE |
| ERREUR | ROUGE |
| DÉBOGUER | BLEU |
| CRITIQUE | Rouge vif |
| TRACER | BLEU |
| DÉFAUT | BLANC |
Vous pouvez utiliser l'un des codes de couleur suivants pour la coloration personnalisée:
| NOM | Code couleur |
|---|---|
| Cyan | Cyan |
| JAUNE | JAUNE |
| ROUGE | ROUGE |
| BLEU | BLEU |
| Rouge vif | CRITIQUE |
| BLANC | BLANC |
Par exemple, vous pouvez utiliser color=logly.COLOR.RED pour la couleur rouge.
Si vous souhaitez utiliser Logly dans vos fichiers de projet sans créer un nouvel objet dans chaque fichier ou classe Python, vous pouvez créer un fichier nommé logly.py. Dans ce fichier, initialisez logly et configurez les valeurs par défaut. Maintenant, vous pouvez facilement l'importer et l'utiliser tout au long de votre projet:
logly.py
# logly.py in your root or custom path
# Import Logly
from logly import Logly
import os
logly = Logly ()
logly . start_logging ()
# Set the default file path and maximum file size
logly . set_default_max_file_size ( 50 )
logger = os . path . join ( os . path . dirname ( os . path . abspath ( __file__ )), "log.txt" ) # This will ensure the path location to create the log.txt
logly . set_default_file_path ( logger )
# Start logging again
logly . start_logging ()vous pouvez maintenant utiliser le logly par
main.py
from logly import logly # make sure to import it some IDE may automatically import it on top
logly . info ( "msg" , "hello this is logly" , color = logly . COLOR . RED ) # with custom color of red [XXXX-XX-XX XX:XX: XX] INFo: msg: hello this is logly
Les contributions sont les bienvenues! Avant de contribuer, veuillez lire nos lignes directrices contributives pour assurer un processus de développement en douceur et collaboratif.
Veuillez consulter notre code de conduite pour comprendre les normes de comportement que nous attendons des contributeurs et des utilisateurs de ce projet.
Ce projet est autorisé sous la licence du MIT. Voir la licence pour plus de détails.
Soutenez le projet en devenant sponsor sur GitHub