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:
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?
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.
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.
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)
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
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:
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.
You can found an example how to enable the Admin interface on Github.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With