Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic log level changes with Logback in a docker app

Logback has a feature to scan changes in logback.xml (per this) - this is a great feature which allows long running applications to be shipped with INFO as default level to be changed to DEBUG when something has to be briefly investigated.

But in my recent app (hosted on my company's K8s cluster as a Docker container), am unable to use the above feature because:

  • dont yet have the provision to host external volumes wherein I can put my logback.xml (to be changed by developers during debugging)
  • dont yet have the provision to change environment variables in a running container without a restart
  • dont have a JNDI server to map log variables

Given this, is there a way I can build a provision for developers to change the logging level of my application at runtime without an app/container restart?

like image 946
Bharadwaj Avatar asked May 02 '20 21:05

Bharadwaj


People also ask

How do I change the log level in a docker container?

You can run the command kubectl exec -it <container_name> bash and use the command line inside the container to change the environment variable . You can do it by running the command export LOG_LEVEL=debug or export LOG_LEVEL=error inside the container.

How do you create a dynamic log level?

As described by Aaron, you can set the log level programmatically. You can implement it in your application in the way you would like it to happen. For example, you could have a GUI where the user or admin changes the log level and then call the setLevel() methods on the logger.

How do you change the logging level?

To change log levels as a root user, perform the following: To enable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=DEBUG) To disable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=INFO)


2 Answers

I'm not sure if you're also forbidden to mount configmap into a container, as you haven't mentioned it.
But on the off chance that you're not, you can create a configmap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: logback-conf
data:
  logback.xml: |
    <configuration>
      <root level="debug">
        <appender-ref ref="STDOUT" />
      </root>
    </configuration>

And add it to the container, as follows:

  containers:
    - name: app
      volumeMounts:
      - name: config
        # The conf will be mounted at /app/logback.xml
        mountPath: /app
...
  volumes:
    - name: config
      configMap:
        name: logback-conf

If you aren't allowed to even inject a configmap (which to my knowledge doesn't cause downtime), there's really not much you can do

like image 154
Nicolai Schmid Avatar answered Oct 12 '22 21:10

Nicolai Schmid


I have a similar problem: I need to change the application log level at runtime without restart the spring boot application. In my situation, I'm not working in a docker context, but I think the solution to our problem is the same.

After research I found 3 possible solutions:

  1. change log file at runtime
  2. enable JMX and work with JConsole to change log levels
  3. use Spring Boot Admin.

The 3 solution is the best, if your app is a Spring Boot app. You can read the following tutorial Changing the logging level at runtime.

spring boot admin

You can found an example how to enable the Admin interface on Github.

like image 37
xcesco Avatar answered Oct 12 '22 23:10

xcesco