英文:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论