日志文件未创建

huangapple go评论84阅读模式
英文:

LogFile is not created

问题

我正在使用Go语言。
我想在运行go run main.go时创建一个名为webapp.log的日志文件。
然而,当我运行go run main.go时,终端中出现了消息"2021/09/25 14:58:55 open : no such file or directory",并且日志文件没有被创建。
从这个消息中我无法确定是哪个位置导致了日志文件没有被创建。
如果有人可以帮助我,请告诉我。
以下是要翻译的代码部分:

main.go

package main

import (
	"fmt"
	"golang_todo_app/config"
	"log"
)

func main() {
  fmt.Println(config.Config.Port)
  fmt.Println(config.Config.SQLDriver)
  fmt.Println(config.Config.DbName)
  fmt.Println(config.Config.LogFile)
  log.Println("test")
}

config/config.go

package config

import (
	"golang_todo_app/utils"
	"log"

	"gopkg.in/go-ini/ini.v1"
)

type ConfigList struct {
	Port string
	SQLDriver string
	DbName string
	LogFile string
}

var Config ConfigList

func init () {
  LoadConfig()
  utils.LoggingSettings(Config.LogFile)
}

func LoadConfig() {
  cfg, err := ini.Load("config.ini")
  if err != nil {
    log.Fatalln()
  }
  Config = ConfigList{
    Port: cfg.Section("web").Key("port").MustString("8080"),
    SQLDriver: cfg.Section("db").Key("driver").String(),
    DbName: cfg.Section("db").Key("name").String(),
    LogFile: cfg.Section("web").Key("logfile").String(),
  }
}

utils/logging.go

package utils

import (
	"io"
	"log"
	"os"
)

func LoggingSettings(logFile string) {
  logfile, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
  if err != nil {
    log.Fatalln(err)
  }
  multiLogFile := io.MultiWriter(os.Stdout, logfile)
  log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  log.SetOutput(multiLogFile)
}

英文:

I am using Go.
I want to create a log file called webapp.log when I run go run main.go.
However, when I run go run main.go, the message "2021/09/25 14:58:55 open : no such file or directory" appears in the terminal and the log file is not created.
I can't tell from the message which location is causing the log file not to be created.
If anyone can help me, please let me know.
main.go

package main

import (
	"fmt"
	"golang_todo_app/config"
	"log"
)

func main() {
  fmt.Println(config.Config.Port)
  fmt.Println(config.Config.SQLDriver)
  fmt.Println(config.Config.DbName)
  fmt.Println(config.Config.LogFile)
  log.Println("test")
}

config/config.go

package config

import (
	"golang_todo_app/utils"
	"log"

	"gopkg.in/go-ini/ini.v1"
)

type ConfigList struct {
	Port string
	SQLDriver string
	DbName string
	LogFile string
}

var Config ConfigList

func init () {
  LoadConfig()
  utils.LoggingSettings(Config.LogFile)
}

func LoadConfig() {
  cfg, err := ini.Load("config.ini")
  if err != nil {
    log.Fatalln()
  }
  Config = ConfigList{
    Port: cfg.Section("web").Key("port").MustString("8080"),
    SQLDriver: cfg.Section("db").Key("driver").String(),
    DbName: cfg.Section("db").Key("name").String(),
    LogFile: cfg.Section("web").Key("logfile").String(),
  }
}

utils/logging.go

package utils

import (
	"io"
	"log"
	"os"
)

func LoggingSettings(logFile string) {
  logfile, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
  if err != nil {
    log.Fatalln(err)
  }
  multiLogFile := io.MultiWriter(os.Stdout, logfile)
  log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  log.SetOutput(multiLogFile)
}

答案1

得分: 1

看一下你的代码,最可能引发此错误的地方是下面这段代码:

logfile, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)

通常,如果我们以没有权限的方式打开一个文件,比如/root/log.txt,会产生如下错误:

2021/09/25 17:50:07 open /root/log.txt: permission denied

你可以看到/root/log.txt会被显示出来,告诉用户哪个文件无法打开,但是对于你的情况,open后面没有返回任何内容。这意味着对于下面的代码:

LoggingSettings(Config.LogFile)

Config.LogFile解析器没有返回任何内容。正确的ini设置可能如下:

config.ini:

[web]
logfile=/tmp/log.txt

你应该检查你的ini文件,看看是否有任何错误导致代码无法解析有效的logfile值。

顺便说一下,如果你导入了"golang_todo_app/config",那么你在main函数中的打印语句将在下一个函数之后执行,如果错误发生在init函数中,你在main函数中的打印语句将没有机会运行。

func init() {
  LoadConfig()
  utils.LoggingSettings(Config.LogFile)
}
英文:

> "2021/09/25 14:58:55 open : no such file or directory"

Have a look of your code, the most possible place which will raise this error is next:

logfile, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)

Usually if we open a file with no permission e.g. /root/log.txt, it could produce next:

2021/09/25 17:50:07 open /root/log.txt: permission denied

You could see the /root/log.txt will be displayed to tell user which file can't be open, but for you, your error return nothing after open. This means for next code:

LoggingSettings(Config.LogFile)

The Config.LogFile parser returns nothing. The correct ini setting maybe next:

config.ini:

[web]
logfile=/tmp/log.txt

You should check your ini file to see if any mistake makes the code can't parse a valid logfile value.

BTW, next code will happen when you import "golang_todo_app/config", so your print in main function executed after next function, your print in main won't have chance to run if the error haapens in init.

func init () {
  LoadConfig()
  utils.LoggingSettings(Config.LogFile)
}

huangapple
  • 本文由 发表于 2021年9月25日 17:17:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/69324760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定