파이썬 응용 프로그램에 대한 맞춤형 로깅 코드를 작성하는 데 지쳤습니까?
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 사용할 수 있습니다.
각 Python 파일 또는 클래스에서 새 개체를 작성하지 않고 프로젝트 파일에 로그리를 사용하려면 logly.py라는 파일을 만들 수 있습니다. 이 파일에서는 로그를 초기화하고 기본값을 구성하십시오. 이제 프로젝트 전체에서 쉽게 가져오고 사용할 수 있습니다.
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 ()이제 로그를 사용할 수 있습니다
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의 스폰서가되어 프로젝트를 지원하십시오