Change the time format while logging in ROS2

Hi,

While I am logging, the time is timestamp. It is not easy to read.

Do you know how to configure the time to format YY-MM-DD HH-MM-SS?

Thanks

ROS 2 uses Python’s standard logging module. You can configure it to display timestamps in different ways.

You can configure the logging format globally at the beginning of your node:

import logging

# Configure logging format to include human-readable timestamps
logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s',
                    level=logging.INFO,
                    datefmt='%Y-%m-%d %H:%M:%S')

then, you can use get_logger() with that configuration.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.

Hi, I was busy so could not come back sooner.
This time I will keep up until the end

I try your suggestion but it does not work.
It still prints the float time.

Could you check my code?

#! /usr/bin/env python3

import rclpy
from rclpy.node import Node
from rclpy.qos import QoSProfile, QoSReliabilityPolicy, QoSDurabilityPolicy

from std_msgs.msg import Int32

import os, sys, argparse
from pathlib import Path
from glob import glob
import time
from datetime import datetime
import logging


logging.basicConfig(
    level       = logging.INFO,
    format      = "[%(process)d] [%(levelname)s] [%(asctime)s] [%(name)s] : %(message)s"
)


class TopicPublisher(Node):
    def __init__(self, nodeName):
        self.nodeName = nodeName
        super().__init__(node_name=self.nodeName)
        self.get_logger().set_level(rclpy.logging.LoggingSeverity.INFO)
        self.ctrlC = False

        # region init
        self.publisher  = self.create_publisher(
            msg_type    = Int32, 
            topic       = 'topic_publisher', 
            qos_profile = QoSProfile(
                depth = 10,
                reliability = QoSReliabilityPolicy.RELIABLE,    # BEST_EFFORT: sensor | RELIABLE: control
                durability  = QoSDurabilityPolicy.TRANSIENT_LOCAL     # TRANSIENT_LOCAL: latch | VOLATILE: no latch
            )
        )
        self.int32_publisher = Int32()

        self.timer = self.create_timer(2, self.timer_callback)
        # endregion

    def timer_callback(self):
        self.int32_publisher.data = self.int32_publisher.data + 1
    
        self.publisher.publish(self.int32_publisher)
        self.get_logger().info(f"self.int32_publisher.data: {self.int32_publisher.data}")


def main(args=None):
    rclpy.init(args=args)

    topicPublisher = TopicPublisher("topic_publisher_node")
    rclpy.spin(topicPublisher)

    topicPublisher.destroy_node()
    rclpy.try_shutdown()
    
        
if __name__ == '__main__':
    main()

    # for key, value in os.environ.items():
    #     print(f"{key} -- {value}")

Please check and help

Thanks

well this is because you are importing logging but not using it. Since you are configuring that and not the logger integrated within the ROS 2 node, I’m not sure you can just use get_loger().info and have it work with the correct format.

You would need to actually use logging like this:

logger = logging.getLogger(__name__)
logger.info('This is an info message')
logger.error('An error occurred')

If I use the logger from logging library like you said:

logger = logging.getLogger(name)
logger.info(‘This is an info message’)
logger.error(‘An error occurred’)

I can have the message like I want but when I check in RosConsole tool, it does not show these messages. RosConsole just show messages that log by self.get_logger().info.

So I want to keep logging normally but only changing the time format for checking issue easily later

This topic was automatically closed after 6 days. New replies are no longer allowed.

Does anyone help me, please? ^^

You should check how to configure RosConsole:

1 Like