使用mpy的logging模组记录日志

1.前言

为了记录程序运行过程中的信息,便想着找一个能记录日志文件的模组,在cPython中常用的日志记录模组是logging,但是mpy不一定有,便去mpy官方模组托管库(https://github.com/micropython/micropython-lib.git)查了下,发现已经有了,位于python-stdlib\logging目录中,但是查看源码以及例子发现功能很简单,没有文件记录功能。只好自己手动完善一下了。参考了cPython的logging使用方法,将日志记录相关简单实现了下,可以兼容。

2.安装使用

我整理好的logging模组已经上传到了我的gitee仓库:https://gitee.com/l_y_r/mpy_lib.git
下载下来将其中的logging.py上传到你的linux设备中,并仿照example_logging.py使用

  1. 简单使用
    import logging
    logging.debug('debug message')
    logging.info('info message')
    logging.warning('warn message')
    logging.error('error message')
    logging.critical('critical message')
    
  2. 记录到文件
    import logging
    logging.basicConfig(level=logging.DEBUG,filename='/logger.log', format='%(asctime)s :  %(message)s')
    logging.debug('debug message should go to the log file')
    logging.info('info message should go to the log file')
    logging.warning('warn message should go to the log file')
    logging.error('error message should go to the log file')
    logging.critical('critical message should go to the log file')
    
  3. 记录到文件同时打印到标准输出
    import logging
    logger=logging.getLogger()
    
    fh = logging.FileHandler('/logger.log')
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    logger.setLevel(level = logging.DEBUG)
    logger.addHandler(fh)
    
    logger.debug('debug message')
    logger.info('info message')
    

3.log记录的一些配置

3.1Formatter 格式化器

用来控制打印的格式,常用的格式关键字如下:

  • asctime: 记录时间
  • name: 记录者,初始化getLogger([name])时传入,默认为root
  • levelname: 日志级别,(debug, info, waring, error, critical)
  • message: 记录的信息

格式关键字使用%()s的形式引用,例如format='%(asctime)s : %(message)s'

好的,了解了您的需求。树莓派pico可以通过GPIO来控制车辆的电机、舵机等部件,而mpyMicroPython的缩写,是一种精简版的Python解释器,可以在树莓派pico上运行。 下面是一个简单的mpy程序,用于通过遥控器控制小车前进、后退、左转、右转等操作: ```python import machine import utime # 设置GPIO引脚 motor_a1 = machine.Pin(0, machine.Pin.OUT) motor_a2 = machine.Pin(1, machine.Pin.OUT) motor_b1 = machine.Pin(2, machine.Pin.OUT) motor_b2 = machine.Pin(3, machine.Pin.OUT) # 设置PWM引脚 pwm_a = machine.PWM(machine.Pin(4)) pwm_b = machine.PWM(machine.Pin(5)) # 设置红外接收器引脚 ir_receiver = machine.Pin(6, machine.Pin.IN) # 设置PWM周期和占空比 pwm_a.freq(1000) pwm_b.freq(1000) pwm_a.duty_u16(0) pwm_b.duty_u16(0) # 定义小车前进函数 def forward(): motor_a1.value(1) motor_a2.value(0) motor_b1.value(1) motor_b2.value(0) pwm_a.duty_u16(50000) pwm_b.duty_u16(50000) # 定义小车后退函数 def backward(): motor_a1.value(0) motor_a2.value(1) motor_b1.value(0) motor_b2.value(1) pwm_a.duty_u16(50000) pwm_b.duty_u16(50000) # 定义小车左转函数 def left(): motor_a1.value(0) motor_a2.value(1) motor_b1.value(1) motor_b2.value(0) pwm_a.duty_u16(30000) pwm_b.duty_u16(30000) # 定义小车右转函数 def right(): motor_a1.value(1) motor_a2.value(0) motor_b1.value(0) motor_b2.value(1) pwm_a.duty_u16(30000) pwm_b.duty_u16(30000) # 定义小车停止函数 def stop(): motor_a1.value(0) motor_a2.value(0) motor_b1.value(0) motor_b2.value(0) pwm_a.duty_u16(0) pwm_b.duty_u16(0) # 定义红外接收器中断函数 def ir_callback(pin): if ir_receiver.value() == 1: stop() else: forward() # 绑定红外接收器中断函数 ir_receiver.irq(trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING, handler=ir_callback) # 主循环 while True: # 检测按键状态,控制小车运动 if button_a.is_pressed(): forward() elif button_b.is_pressed(): backward() elif button_c.is_pressed(): left() elif button_d.is_pressed(): right() else: stop() utime.sleep_ms(10) ``` 这只是一个简单的示例程序,您可以根据自己的需求对其进行修改和扩展。同时,您需要配合硬件来实现小车的运动控制,包括电机、舵机、遥控器等。希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值