dscc90150010 2019-07-22 08:14
浏览 630

更改容器运行时的日志级别

im using logrus for logging for out applications which run on K8S we have env variable which we can set the log-level 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 error to debug, I think this is legitimic request but didn’t find any reference or any open source which doing this, any idea?

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 := &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))
    }
}

I know how to change the log level but I need a way to infuance the logger to change the level

  • 写回答

3条回答 默认 最新

  • dro60731 2019-07-22 08:20
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划