golang read log file contents inside a directory

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

golang read log file contents inside a directory

问题

我正在尝试读取目录中的所有日志文件,下面的代码可以读取文件名,但无法读取其内容。

控制台输出:

ds-api-doc-.log false
2023/03/21 11:27:11 open /Users/xxx/ds-api-doc.log: no such file or directory

代码如下:

files, err := ioutil.ReadDir("./logs/")
if err != nil {
    log.Fatal(err)
}
fmt.Println(totalTicks)
for _, file := range files {
    fmt.Println(file.Name(), file.IsDir())
    abs, err := filepath.Abs(file.Name())

    file, err := os.Open(abs)
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        fmt.Println(scanner.Text())
    }

    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }
}

请注意,这只是读取文件内容的部分代码,可能还有其他问题需要解决。

英文:

i'm trying to read all log files inside a directory, Below code is able to read the filename but not its contents.

console output

ds-api-doc-.log false
2023/03/21 11:27:11 open /Users/xxx/ds-api-doc.log: no such file or directory 







 files, err := ioutil.ReadDir("./logs/")
        	if err != nil {
        		log.Fatal(err)
        	}
        	fmt.Println(totalTicks)
        	for _, file := range files {
        		fmt.Println(file.Name(), file.IsDir())
        		abs, err := filepath.Abs(file.Name())
        
        		file, err := os.Open(abs)
        		if err != nil {
        			log.Fatal(err)
        		}
        		defer file.Close()
        
        		scanner := bufio.NewScanner(file)
        		for scanner.Scan() {
        			fmt.Println(scanner.Text())
        		}
        
        		if err := scanner.Err(); err != nil {
        			log.Fatal(err)
        		}
        
        	}

答案1

得分: 1

file.Name() 仅返回文件的基本名称。

filepath.Abs() 将给定的路径(在这种情况下是文件的基本名称)与当前工作目录连接起来。因此,返回的 abs 值将缺少文件路径中的 ./logs/ 部分。

要修复这个问题,你可以执行以下操作:

abs, err := filepath.Abs(filepath.Join("logs", file.Name()))

或者你可以使用 filepath.WalkDir,它将文件的路径提供给 fn 参数。

err := filepath.WalkDir("./logs/", func(path string, de fs.DirEntry, err error) error) {
    if err != nil {
        return err
    } else if de.IsDir() {
        return nil
    }

    file, err := os.Open(path)
    // ...
    return nil
}) 
英文:

file.Name() returns only the base name of the file.

filepath.Abs() will join the given path, in this case the file's base name, to the current working directory. Therefore the returned abs value will be missing the ./logs/ segment of the file's path.

To fix that you can do the following:

abs, err := filepath.Abs(filepath.Join("logs", file.Name()))

Alternatively you can use filepath.WalkDir, which provides the file's path to the fn argument.

err := filepath.WalkDir("./logs/", func(path string, de fs.DirEntry, err error) error) {
    if err != nil {
        return err
    } else if de.IsDir() {
        return nil
    }

    file, err := os.Open(path)
    // ...
    return nil
}) 

huangapple
  • 本文由 发表于 2023年3月21日 14:04:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75797758.html
匿名

发表评论

匿名网友

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

确定