logly
v0.0.6
厭倦了為Python應用程序編寫自定義記錄代碼?
logly是一種準備登錄實用程序,它提供了一種簡單的方法來記錄不同級別,顏色和許多自定義選項的消息。它旨在靈活,使您可以根據應用程序的需求自定義日誌消息。 Logly支持日誌記錄到控制台和文件,並帶有內置的顏色編碼日誌級別,以提高可見性。
如果您喜歡這個項目,請確保出演?它在存儲庫中,如果您想貢獻,請確保為此存儲庫提供。
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模塊導入Logly類。Logly的實例。start_logging()方法開始記錄。stop_logging()方法記錄。有關更多信息,請檢查存儲庫
如果遇到與默認文件路徑相關的錯誤,則可以使用以下代碼段來設置默認路徑:
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 )這將設置默認文件路徑,您可以根據自己的要求對其進行自定義。
如果要設置日誌文件的默認路徑,則可以使用以下代碼段
from logly import Logly
logly = Logly ()
logly . set_default_file_path ( "log.txt" )如果您遇到了諸如FileNotFoundError: [Errno 2] No such file or directory: 'log.txt'您可以使用以下代碼段來設置默認路徑
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 )有關更多信息,請檢查存儲庫。
| 等級 | 顏色代碼 |
|---|---|
| 資訊 | 青色 |
| 警告 | 黃色的 |
| 錯誤 | 紅色的 |
| 偵錯 | 藍色的 |
| 批判的 | 鮮紅色 |
| 痕跡 | 藍色的 |
| 預設 | 白色的 |
您可以使用以下任何顏色代碼進行自定義著色:
| 姓名 | 顏色代碼 |
|---|---|
| 青色 | 青色 |
| 黃色的 | 黃色的 |
| 紅色的 | 紅色的 |
| 藍色的 | 藍色的 |
| 鮮紅色 | 批判的 |
| 白色的 | 白色的 |
例如,您可以使用color=logly.COLOR.RED進行紅色。
如果要在項目文件中使用Logly而不在每個Python文件或類中創建新對象,則可以創建一個名為Logly.py的文件。在此文件中,初始化logly並配置默認值。現在,您可以在整個項目中輕鬆導入並使用它:
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 ()您現在可以使用Logly
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
歡迎捐款!在貢獻之前,請閱讀我們的貢獻指南,以確保平穩和協作的開發過程。
請查看我們的行為準則,以了解我們期望該項目的貢獻者和用戶的行為標準。
該項目已根據MIT許可獲得許可。有關更多詳細信息,請參見許可證。
通過成為GitHub的讚助商來支持該項目