In python để đăng nhập

Nếu bạn muốn in nhật ký python trong một tệp thay vì trên bảng điều khiển thì chúng ta có thể làm như vậy bằng cách sử dụng phương thức basicConfig() bằng cách cung cấp tham số filenamefilemode

Định dạng của tin nhắn có thể được chỉ định bằng cách sử dụng tham số format trong phương thức basicConfig()

Hãy để chúng tôi lấy một ví dụ cơ bản để in nhật ký vào một tệp thay vì trên bàn điều khiển. Đoạn mã được đưa ra dưới đây

import logging    # first of all import the module

logging.basicConfig(filename='std.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s')
logging.warning('This message will get logged on to a file')

root - LỖI - Thông báo này sẽ được đăng nhập vào một tệp

Đầu ra ở trên cho thấy thông báo sẽ trông như thế nào nhưng hãy nhớ rằng nó sẽ được ghi vào một tệp có tên std. log thay vì bảng điều khiển

Trong đoạn mã trên, filemode được đặt thành

#importing the module 
import logging 

#now we will Create and configure logger 
logging.basicConfig(filename="std.log", 
					format='%(asctime)s %(message)s', 
					filemode='w') 

#Let us Create an object 
logger=logging.getLogger() 

#Now we are going to Set the threshold of logger to DEBUG 
logger.setLevel(logging.DEBUG) 

#some messages to test
logger.debug("This is just a harmless debug message") 
logger.info("This is just an information for you") 
logger.warning("OOPS!!!Its a Warning") 
logger.error("Have you try to divide a number by zero") 
logger.critical("The Internet is not working....") 
1, có nghĩa là tệp nhật ký được mở ở “chế độ ghi” mỗi khi gọi basicConfig() và sau mỗi lần chạy chương trình, nó sẽ ghi lại tệp

Cấu hình mặc định cho chế độ tệp là

#importing the module 
import logging 

#now we will Create and configure logger 
logging.basicConfig(filename="std.log", 
					format='%(asctime)s %(message)s', 
					filemode='w') 

#Let us Create an object 
logger=logging.getLogger() 

#Now we are going to Set the threshold of logger to DEBUG 
logger.setLevel(logging.DEBUG) 

#some messages to test
logger.debug("This is just a harmless debug message") 
logger.info("This is just an information for you") 
logger.warning("OOPS!!!Its a Warning") 
logger.error("Have you try to divide a number by zero") 
logger.critical("The Internet is not working....") 
0, đó là append, có nghĩa là các bản ghi sẽ được thêm vào tệp nhật ký và thêm nhật ký vào các bản ghi hiện có

Ghi nhật ký Python - Lưu trữ nhật ký trong một tệp

Có một số bước cơ bản và chúng được đưa ra dưới đây

  1. Trước hết, chỉ cần nhập mô-đun ghi nhật ký bằng cách viết

    #importing the module 
    import logging 
    
    #now we will Create and configure logger 
    logging.basicConfig(filename="std.log", 
    					format='%(asctime)s %(message)s', 
    					filemode='w') 
    
    #Let us Create an object 
    logger=logging.getLogger() 
    
    #Now we are going to Set the threshold of logger to DEBUG 
    logger.setLevel(logging.DEBUG) 
    
    #some messages to test
    logger.debug("This is just a harmless debug message") 
    logger.info("This is just an information for you") 
    logger.warning("OOPS!!!Its a Warning") 
    logger.error("Have you try to divide a number by zero") 
    logger.critical("The Internet is not working....") 
    
    1

  2. Bước thứ hai là tạo và cấu hình bộ ghi. Để định cấu hình bộ ghi để lưu nhật ký trong một tệp, bắt buộc phải chuyển tên của tệp mà bạn muốn ghi lại các sự kiện

  3. Trong bước thứ ba, định dạng của bộ ghi cũng có thể được đặt. Lưu ý rằng theo mặc định, tệp hoạt động ở chế độ chắp thêm nhưng chúng tôi có thể thay đổi chế độ đó sang chế độ ghi nếu cần

  4. Bạn cũng có thể đặt cấp độ của bộ ghi

Vì vậy, hãy chuyển sang mã ngay bây giờ

#importing the module 
import logging 

#now we will Create and configure logger 
logging.basicConfig(filename="std.log", 
					format='%(asctime)s %(message)s', 
					filemode='w') 

#Let us Create an object 
logger=logging.getLogger() 

#Now we are going to Set the threshold of logger to DEBUG 
logger.setLevel(logging.DEBUG) 

#some messages to test
logger.debug("This is just a harmless debug message") 
logger.info("This is just an information for you") 
logger.warning("OOPS!!!Its a Warning") 
logger.error("Have you try to divide a number by zero") 
logger.critical("The Internet is not working....") 

Đoạn mã trên sẽ ghi một số thông báo vào tệp có tên std. đăng nhập. Nếu chúng ta mở tệp thì các thông báo sẽ được viết như sau

2020-06-19 12. 48. 00,449 - Đây chỉ là thông báo gỡ lỗi vô hại 2020-06-19 12. 48. 00,449 - Đây chỉ là thông tin cho bạn 2020-06-19 12. 48. 00,449 - LẠI. Đó là một cảnh báo 2020-06-19 12. 48. 00,449 - Bạn thử chia một số cho 0 2020-06-19 12. 48. 00,449 - Internet không hoạt động

Bạn có thể thay đổi định dạng nhật ký, cấp độ nhật ký hoặc bất kỳ thuộc tính nào khác của

#importing the module 
import logging 

#now we will Create and configure logger 
logging.basicConfig(filename="std.log", 
					format='%(asctime)s %(message)s', 
					filemode='w') 

#Let us Create an object 
logger=logging.getLogger() 

#Now we are going to Set the threshold of logger to DEBUG 
logger.setLevel(logging.DEBUG) 

#some messages to test
logger.debug("This is just a harmless debug message") 
logger.info("This is just an information for you") 
logger.warning("OOPS!!!Its a Warning") 
logger.error("Have you try to divide a number by zero") 
logger.critical("The Internet is not working....") 
2 cùng với việc đặt tên tệp để lưu trữ nhật ký trong một tệp cùng với chế độ