Skip to content

Commit

Permalink
Add file check before running
Browse files Browse the repository at this point in the history
  • Loading branch information
l3dlp committed Aug 10, 2023
1 parent f56b3c6 commit 0018e5c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import "github.com/l3dlp/logfile"
[...]
```go
func main() {
logFile := logfile.LogFile("chemindufichier.log")
defer logFile.Close()
logFile := logfile.Use("chemindufichier.log")
if logFile != nil {
defer logFile.Close()
}
log.Println("blablabla")
}
```

## License

Ce projet est sous licence MIT - voir le fichier [LICENSE](LICENSE) pour plus de détails.
This project is under MIT license - see [LICENSE file](LICENSE) for all details.

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/l3dlp/logfile

go 1.21.0
6 changes: 4 additions & 2 deletions logfile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logfile

import (
"fmt"
"log"
"os"
)
Expand All @@ -10,14 +11,15 @@ var (
logError error
)

func LogFile(logFilePath string) *os.File {
// The function opens a log file, sets it as the output for the log package, and returns the file.
func Use(logFilePath string) *os.File {
logFile, logError = os.OpenFile(
logFilePath,
os.O_RDWR|os.O_CREATE|os.O_APPEND,
0664,
)
if logError != nil {
fmt.Println("Erreur lors de l'ouverture du fichier de log:", err)
fmt.Println("Erreur lors de l'ouverture du fichier de log:", logError)
return nil
}

Expand Down

0 comments on commit 0018e5c

Please sign in to comment.