Python中的日志记录
在Python项目中利用日志记录库,并限制日志文件大小为50MB,同时设置日志文件的滚动计谋,利用logging库和logging.handlers.RotatingFileHandler。以下是详细步骤:
创建一个日志设置文件,例如logging_config.py:
- import logging
- import logging.handlers
- def setup_logging():
- logger = logging.getLogger()
- logger.setLevel(logging.DEBUG)
- # Console handler
- console_handler = logging.StreamHandler()
- console_handler.setLevel(logging.DEBUG)
- console_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
- console_handler.setFormatter(console_formatter)
- logger.addHandler(console_handler)
- # Rotating file handler
- file_handler = logging.handlers.RotatingFileHandler(
- 'D:\\logs\\application.log', maxBytes=50*1024*1024, backupCount=10)
- file_handler.setLevel(logging.DEBUG)
- file_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
- file_handler.setFormatter(file_formatter)
- logger.addHandler(file_handler)
复制代码 在Python代码中导入并利用这个日志设置:
- import logging
- from logging_config import setup_logging
- # 设置日志配置
- setup_logging()
- # 创建日志记录器
- logger = logging.getLogger(__name__)
- def main():
- logger.debug("这是一个调试日志")
- logger.info("这是一个信息日志")
- logger.warning("这是一个警告日志")
- logger.error("这是一个错误日志")
- if __name__ == "__main__":
- main()
复制代码 Kotlin中的日志记录
在Kotlin中,通常利用Logback或Log4j进行日志记录。以下是利用Logback的示例(spring boot 默认):
首先,在build.gradle`文件中添加依靠项:
- dependencies {
- implementation "ch.qos.logback:logback-classic:1.2.3"
- }
复制代码 接下来,创建一个设置文件logback.xml,放在src/main/resources目次中:
- <?xml version="1.0" encoding="UTF-8"?>
- <configuration>
- <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
- <encoder>
- <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
- </encoder>
- </appender>
- <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <file>logs/treevalue-robot-core.log</file>
- <rollingPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
- <maxFileSize>50MB</maxFileSize>
- </rollingPolicy>
- <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
- <maxFileSize>50MB</maxFileSize>
- </triggeringPolicy>
- <encoder>
- <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
- </encoder>
- </appender>
- <appender name="ROLLING_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <file>logs/treevalue-robot-core.log</file>
- <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
- <fileNamePattern>logs/treevalue-robot-core%i.log</fileNamePattern>
- <minIndex>1</minIndex>
- <maxIndex>100</maxIndex>
- </rollingPolicy>
- <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
- <maxFileSize>50MB</maxFileSize>
- </triggeringPolicy>
- <encoder>
- <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
- </encoder>
- </appender>
- <root level="debug">
- <appender-ref ref="CONSOLE"/>
- <appender-ref ref="ROLLING_FILE"/>
- </root>
- </configuration>
复制代码 然后,在Kotlin代码中利用日志记录:
- import org.slf4j.LoggerFactory
- class App {
- private val logger = LoggerFactory.getLogger(App::class.java)
- fun run() {
- System.setProperty("logback.configurationFile", "src/main/resources/logback.xml")
- for (i in 0..5000) {
- logger.debug("这是一个调试日志")
- logger.info("这是一个信息日志")
- logger.warn("这是一个警告日志")
- logger.error("这是一个错误日志")
- }
- }
- }
- fun main() {
- val app = App()
- app.run()
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |