Writing messages to docker logs stream

Hi, I’m the Toradex AI Assistant. The Toradex Support team will follow up with you shortly!

Did you like my answer?

:+1: :-1:


Hello,

To ensure your C++ and Python applications running in Docker containers on Torizon OS write log messages to the Docker logs stream, you need to configure them to output to standard output (stdout) and standard error (stderr), which Docker captures.

For Python Applications:

  1. Use the Logging Module: Configure your Python application to use the logging module to send log messages to stdout or stderr. This can be done by setting up the logging configuration to output to the console.

    Example:

    import logging
    import sys
    
    logging.basicConfig(stream=sys.stdout, level=logging.INFO)
    logger = logging.getLogger(__name__)
    
    logger.info("This is an info message")
    logger.error("This is an error message")
    
  2. Uncaught Exceptions: These are automatically written to stderr, which Docker captures. Ensure other log messages are similarly directed.

For C++ Applications:

  1. Redirect Output to Console: Use std::cout for informational messages and std::cerr for error messages. Docker captures both streams.

    Example:

    #include <iostream>
    
    int main() {
        std::cout << "This is an info message" << std::endl;
        std::cerr << "This is an error message" << std::endl;
        return 0;
    }
    
  2. Avoid Redirecting Output Elsewhere: Ensure your application does not redirect output to files unless intended.

Docker Configuration:

  • Run Container in Foreground: Ensure your Docker container is running in the foreground, as running in detached mode can affect log capturing.
  • Check Logs: Use docker logs <container_id> to view the logs. Verify that the container is running and the application outputs to the correct streams.

By following these steps, your applications should correctly log messages to the Docker logs stream, making them accessible via the docker logs command.

Best regards,
Toradex AI Assistant

Please, also check if these links can help you solve your problem:

How to log persistent from a application running in docker.