<p>im using <a href="https://github.com/sirupsen/logrus" rel="nofollow noreferrer"><code>logrus</code></a> for logging for out applications which run on K8S
we have env variable which we can set the <code>log-level</code> and change it when we restart out application
our applications is running with docker containers on k8s
Now we want to change the log-level on runtime, i.e. don’t restart the container and change it 
when it’s running and with this we can change it from <code>error</code> to <code>debug</code>, I think this 
is legitimic request but didn’t find any reference or any open source which doing this, any idea? </p>

<pre><code>package logs

import (
    "fmt"
    "os"

    "github.com/sirupsen/logrus"
)

const (
    AppLogLevel = “APP_LOG_LEVEL"
    DefLvl = "info"
)


var Logger *logrus.Logger


func NewLogger() *logrus.Logger {

    var level logrus.Level
    lvl := getLogLevel()
    // In case level doesn't set will not print any message
    level = logLevel(lvl)
    logger := &amp;logrus.Logger{
        Out:   os.Stdout,
        Level: level,
    }
    Logger = logger
    return Logger
}

// use from env
func getLogLevel() string {
    lvl, _ := os.LookupEnv(AppLogLevel)
    if lvl != "" {
        return lvl
    }
    return DefLvl
}

func logLevel(lvl string) logrus.Level {

    switch lvl {
    case "debug":
        // Used for tracing
        return logrus.DebugLevel
    case "info":
        return logrus.InfoLevel
    case "error":
        return logrus.ErrorLevel
    case "fatal":
        return logrus.FatalLevel
    default:
        panic(fmt.Sprintf("the specified %s log level is not supported", lvl))
    }
}
</code></pre>

<p>I know how to change the log level but I need a way to infuance the logger to change the level</p>

JME
<p>As a general Un*x statement, you cannot change an environment variable in a process after it has started.  (You can <strong>setenv</strong>(3) your own environment, and you can specify a new process's environment when you <strong>execve</strong>(2) it, but once it's started, you can't change it again.)</p>

<p>This restriction carries through to higher levels.  If you've <code>docker run</code> a container, its <code>-e</code> option to set an environment variable is one of the things you have to delete and recreate a container to change.  The <code>env:</code> is one of the many immutable parts of a Kubernetes Pod specification; you also can't change it without deleting and recreating the pod.</p>

<p>If you've deployed the pod via a Deployment (and you really should), you can change the environment variable setting in the Deployment spec (edit the YAML file in source control and <code>kubectl apply -f</code> it, or directly <code>kubectl edit</code>).  This will cause Kubernetes to start new pods with the new log value and shut down old ones, in that order, doing a zero-downtime update.  <em>Deleting and recreating pods like this is totally normal</em> and happens whenever you want to, for example, change the image inside the deployment to have today's build.</p>

<p>If your application is capable of noticing changes to config files it's loaded (and it would have to be specially coded to do that) one other path that could work for you is to <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#add-configmap-data-to-a-volume" rel="nofollow noreferrer">mount a ConfigMap into a container</a>; if you change the ConfigMap contents, the files the container sees will change but it will not restart.  I wouldn't go out of my way to write this just to avoid restarting a pod, though.</p>
