简体   繁体   English

停止将日志附加到 go.log 文件中

[英]Stop appending logs into go.log file

I have deployed my Go application on Openshift using the Go 1.5.2 cartridge.我已经使用 Go 1.5.2 卡在 Openshift 上部署了我的 Go 应用程序。 As a good practice, I have made it a habit to generate logs about anything that goes down with the application.作为一个很好的做法,我养成了生成有关应用程序出现故障的任何日志的习惯。 But this habit is proving to be costly in the Openshift environment because of the limited storage (1 GB) provided to me.但事实证明,这种习惯在 Openshift 环境中代价高昂,因为我提供的存储空间 (1 GB) 有限。 The logs are easily exceeding the 10 MB mark within a few second of usage, and I fear my application will run out of space if I leave the log file unadministered.日志在使用后的几秒钟内很容易超过 10 MB 标记,如果我不管理日志文件,我担心我的应用程序会耗尽空间。 Currently, I am clearing the log file manually after regular intervals.目前,我在定期间隔后手动清除日志文件。

Is there any way I could stop the appending of logs onto the file, or maybe stop the generation of logs altogether (without affecting my original application code)?有什么办法可以停止将日志附加到文件中,或者完全停止生成日志(而不影响我的原始应用程序代码)? I have tried by revoking write privileges from the file, but the logs keep appearing anyways.我尝试通过撤销文件的写权限,但无论如何日志一直出现。

Ideally, you would want to use a logger that facilitates log levels.理想情况下,您会希望使用便于记录级别的记录器。 This would allow you to adjust the verbosity of logs based on the current runtime context.这将允许您根据当前运行时上下文调整日志的详细程度。 You can look at the How to implement level based logging in golang question for a wealth of information and packages to facilitate log levels.您可以查看如何在 golang问题中实现基于级别的日志记录,以获得丰富的信息和包,以方便使用日志级别。 As a developer, you can include INFO and DEBUG logging statements which, when deployed with a lower log level, will not be included in your logging output.作为开发人员,您可以包含 INFO 和 DEBUG 日志记录语句,当以较低的日志级别部署时,这些语句将不会包含在您的日志记录输出中。

If your in a pinch and need to disable the logging in your current implementation you can configure the native go Logger output writer using the SetOutput(output io.Writer) function.如果您处于紧要关头并且需要在当前实现中禁用日志记录,您可以使用SetOutput(output io.Writer)函数配置本机 go Logger输出编写器。 The default Writer used by the log package is STDERR . log包使用的默认WriterSTDERR The ioutil package provides a top level Discard Writer that you can use to prevent the logger from writing anything. ioutil包提供了一个顶级的Discard Writer,您可以使用它来防止记录器写入任何内容。 You would have to update your application initialization to set the output of the logger based on the environment.您必须更新应用程序初始化以根据环境设置记录器的输出。

package main

import (
    "fmt"
    "io/ioutil"
    "log"
)

func main() {
    var shouldEnableLogging bool = false

    if !shouldEnableLogging {
        fmt.Println("Disabled logging")
        log.SetOutput(ioutil.Discard)
    }

    for i := 0; i < 5; i++ {
        log.Printf("I logging statement %d", i)
    }
}

You can adjust the bool flag in this snippet to see this in action at this code in the Go Playground .您可以调整此代码段中的 bool 标志,以在Go Playground中的此代码中查看此操作。 This will prevent your file space from filling up with logs, but you will loose all logs all together which is not advisable for any application.这将防止您的文件空间被日志填满,但您会将所有日志一起丢失,这对于任何应用程序都是不可取的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM