==============
Container logs
==============

Container logs provide records of activity on the internal components of Package Security Manager—activity like incoming requests, errors, and configuration changes. Checking these logs can help you troubleshoot specific problems with your instance. Logs also give you a clearer picture of how your containers interact with each other. Below is a brief description of each container in the application, and what information you can gain from reviewing the logs for that container.

.. csv-table:: 
         :file: ../csv/container_logs.csv
         :widths: 12, 44, 44
         :header-rows: 1

Viewing containers
==================

Follow these steps to view the containers in your Package Security Manager instance:

#. Open a terminal and connect to your instance of Package Security Manager. 
#. Run the following command: 

    .. code-block:: none

        docker ps --all

Viewing container logs
======================

Follow these steps to view the logs for a specific container:

#. Open a terminal and connect to your instance of Package Security Manager. 
#. Run the following command: 

    .. code-block:: none

        # Replace <CONTAINER_NAME> with the name of the container
        docker logs <CONTAINER_NAME>

    For example: 

    .. code-block:: none

        docker logs keycloak

    .. tip:: 

        You can also view container logs in real-time by adding the ``--follow`` option to your command:

        .. code-block:: none

            # Replace <CONTAINER_NAME> with the name of the container
            docker logs --follow <CONTAINER_NAME>

        Viewing logs in real time allows you to monitor ongoing processes and catch issues as they occur. This is particularly useful during deployments or troubleshooting sessions.

Analyzing container logs
========================

Container logs hold various types of information that can help you understand the behavior and performance of your containers, enabling you troubleshoot and maintain your Package Security Manager instance effectively.

Container logs typically capture the following important details:

.. csv-table:: 
         :file: ../csv/container_log_info.csv
         :widths: 30, 70
         :header-rows: 1

Exporting logs
==============

Save the logs for a specific container by running the following command:

.. code-block:: none

    # Replace <CONTAINER_NAME> with the name of the container you are viewing
    # Replace <FILE_NAME> with the name of the file you are saving
    docker logs <CONTAINER_NAME> > <FILE_NAME>.log

Alternatively, you can collect and save logs for all the containers at once by running the following commands:

    .. code-block:: none
        
        # Replace <LOG_FILE_PREFIX> with a prefix for all of your log files (e.g., logs_dump)
        # Replace <CONTAINER_LOGS_DUMP> with a name for the tarball file that contains all the logs
        for x in $(sudo docker ps --format '{{.Names}}'); do sudo docker logs $x >& <LOG_FILE_PREFIX>_$x; done
        tar cvjf <CONTAINER_LOGS_DUMP>.tar.bz2 <LOG_FILE_PREFIX>_*

Integrating logs with monitoring tools
======================================

Use monitoring tools like Grafana and Prometheus to visualize container performance trends and set up alerts for issues in your Package Security Manager instance. Grafana helps you spot anomalies, while Prometheus allows you to create alerts based on error rates. 

For more information on accessing your dashboards and monitoring the health of your Package Security Manager instance, see :doc:`Grafana monitoring <grafana>`.

To learn more about Prometheus and metrics collection, see :doc:`System metrics with Prometheus <metrics>`.

To configure your logs to be sent directly to AWS CloudWatch, see the official `AWS CloudWatch Logs documentation <https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/WhatIsCloudWatchLogs.html>`_ for detailed instructions.