// Module included in the following assemblies:
//
// * observability/monitoring/configuring-the-monitoring-stack.adoc

:_mod-docs-content-type: PROCEDURE
[id="setting-log-levels-for-monitoring-components_{context}"]
= Setting log levels for monitoring components

// Set attributes to distinguish between cluster monitoring example (core platform monitoring - CPM) and user workload monitoring (UWM) examples

// tag::CPM[]
:configmap-name: cluster-monitoring-config
:namespace-name: openshift-monitoring
:prometheus: prometheusK8s
:alertmanager: alertmanagerMain
:thanos: thanosQuerier
:component-name: Thanos Querier
// end::CPM[]
// tag::UWM[]
:configmap-name: user-workload-monitoring-config
:namespace-name: openshift-user-workload-monitoring
:prometheus: prometheus
:alertmanager: alertmanager
:thanos: thanosRuler
:component-name: Thanos Ruler
// end::UWM[]

// tag::CPM[]
You can configure the log level for Alertmanager, Prometheus Operator, Prometheus, and {component-name} and log verbosity for Metrics Server.
// end::CPM[]
// tag::UWM[]
You can configure the log level for Alertmanager, Prometheus Operator, Prometheus, and {component-name}.
// end::UWM[]
You can use these settings for troubleshooting and to gain better insight into how the components are functioning.

The following log levels can be applied to the relevant component in the `{configmap-name}` `ConfigMap` object:

* `debug`. Log debug, informational, warning, and error messages.
* `info` (default). Log informational, warning, and error messages.
* `warn`. Log warning and error messages only.
* `error`. Log error messages only.

.Prerequisites

// tag::CPM[]
* You have access to the cluster as a user with the `cluster-admin` cluster role.
* You have created the `cluster-monitoring-config` `ConfigMap` object.
// end::CPM[]
// tag::UWM[]
ifndef::openshift-dedicated,openshift-rosa[]
* You have access to the cluster as a user with the `cluster-admin` cluster role or as a user with the `user-workload-monitoring-config-edit` role in the `openshift-user-workload-monitoring` project.
* A cluster administrator has enabled monitoring for user-defined projects.
endif::openshift-dedicated,openshift-rosa[]

ifdef::openshift-dedicated,openshift-rosa[]
* You have access to the cluster as a user with the `dedicated-admin` role.
* The `user-workload-monitoring-config` `ConfigMap` object exists. This object is created by default when the cluster is created.
endif::openshift-dedicated,openshift-rosa[]
// end::UWM[]
* You have installed the {oc-first}.

.Procedure

. Edit the `{configmap-name}` config map in the `{namespace-name}` project:
+
[source,terminal,subs="attributes+"]
----
$ oc -n {namespace-name} edit configmap {configmap-name}
----

. Add log configuration for a component under `data/config.yaml`:
+
[source,yaml,subs="attributes+"]
----
apiVersion: v1
kind: ConfigMap
metadata:
  name: {configmap-name}
  namespace: {namespace-name}
data:
  config.yaml: |
    <component>: # <1>
      logLevel: <log_level> # <2>
# tag::CPM[]
    metricsServer:
      verbosity: <value> # <3>
# end::CPM[]
    # ...
----
<1> Specify the monitoring stack component for which you are setting a log level.
Available component values are `{prometheus}`, `{alertmanager}`, `prometheusOperator`, and `{thanos}`.
<2> Specify the log level for the component.
The available values are `error`, `warn`, `info`, and `debug`.
The default value is `info`.
// tag::CPM[]
<3> Specify the verbosity for Metrics Server.
Valid values are positive integers.
Increasing the number increases the amount of logged events, values over `10` are usually unnecessary.
The default value is `0`.
// end::CPM[]

. Save the file to apply the changes. The pods affected by the new configuration are automatically redeployed.

. Verify that the log configuration is applied by reviewing the deployment or pod configuration in the related project. 

** The following example checks the log level for the `prometheus-operator` deployment:
+
[source,terminal,subs="attributes+"]
----
$ oc -n {namespace-name} get deploy prometheus-operator -o yaml | grep "log-level"
----
+
.Example output
[source,terminal]
----
        - --log-level=debug
----

// tag::CPM[]
** The following example checks the log verbosity for the `metrics-server` deployment:
+
[source,terminal]
----
$ oc -n openshift-monitoring get deploy metrics-server -o yaml | grep -- '--v='
----
+
.Example output
[source,terminal]
----
        - --v=3
----
// end::CPM[]

. Verify that the pods for the component are running:
+
[source,terminal,subs="attributes+"]
----
$ oc -n {namespace-name} get pods
----
+
[NOTE]
====
If an unrecognized `logLevel` value is included in the `ConfigMap` object, the pods for the component might not restart successfully.
====

// Unset the source code block attributes just to be safe.
:!configmap-name:
:!namespace-name:
:!prometheus:
:!alertmanager:
:!thanos:
:!component-name:
