Skip to content

Commit

Permalink
Add option to disable using current time as logfile name
Browse files Browse the repository at this point in the history
  • Loading branch information
yunginnanet committed Sep 15, 2021
1 parent d5677d1 commit 492a696
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 15 deletions.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion cmd/main.go → cmd/HellPot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func init() {
if config.Debug {
log.Debug().Msg("debug enabled")
}
log.Info().Str("config", config.Filename).Msg("initialization finished")
log.Info().Str("file", config.Filename).Msg("current config")
log.Info().Str("file", config.CurrentLogFile).Msg("current log")
}

func main() {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 11 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,24 @@ func Init() {
snek.AddConfigPath(loc)
}

Filename = snek.ConfigFileUsed()

if err = snek.MergeInConfig(); err != nil {
if _, err := os.Stat(prefConfigLocation); os.IsNotExist(err) {
if err = os.Mkdir(prefConfigLocation, 0755); err != nil {
panic(err)
}
}
if err = snek.SafeWriteConfigAs(prefConfigLocation + "/" + "config.toml"); err != nil {

newconfig := prefConfigLocation + "/" + "config.toml"

if err = snek.SafeWriteConfigAs(newconfig); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}

Filename = snek.ConfigFileUsed()
Filename = newconfig
}

associate()
}
Expand All @@ -104,12 +109,9 @@ func setDefaults() {
var configSections = []string{"logger", "http"}

Opt["logger"] = map[string]interface{}{
"debug": true,
"directory": home + "/.config/" + title + "/logs/",
}
Opt["data"] = map[string]interface{}{
"directory": home + "/.config/" + title + "./.data/",
"maxsizeobj": 20,
"debug": true,
"directory": home + "/.config/" + title + "/logs/",
"use_date_filename": true,
}
Opt["http"] = map[string]interface{}{
"bind_addr": "127.0.0.1",
Expand Down
22 changes: 17 additions & 5 deletions config/logger.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
package config

import (
"github.com/rs/zerolog"
"os"
"strings"
"time"

"github.com/rs/zerolog"
)

var (
logFile *os.File
logDir string
logFile *os.File
logDir string
CurrentLogFile string
)

// Logger retrieves an instance of our zerolog loggger so we can hook it in our main package.
func Logger() zerolog.Logger {
logDir = snek.GetString("logger.directory")
if err := os.MkdirAll(logDir, 0755); err != nil {
println("cannot create log directory: " + logDir + "(" + err.Error() + ")")
os.Exit(1)
}
if logFile, err = os.OpenFile(logDir+time.Now().Format(time.RFC3339)+".log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err != nil {

tnow := "HellPot"

if snek.GetBool("logger.use_date_filename") {
tnow = strings.Replace(time.Now().Format(time.RFC822), " ", "_", -1)
tnow = strings.Replace(tnow, ":", "-", -1)
}

CurrentLogFile = logDir + tnow + ".log"

if logFile, err = os.OpenFile(CurrentLogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err != nil {
println("cannot create log file: " + err.Error())
os.Exit(1)
}
Expand Down

0 comments on commit 492a696

Please sign in to comment.