เบื่อที่จะเขียนรหัสการบันทึกที่กำหนดเองสำหรับแอปพลิเคชัน 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 จากโมดูล loglyLoglystart_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