Improve logging
This commit is contained in:
parent
57903d4724
commit
ca9b6e05b7
10 changed files with 49 additions and 21 deletions
|
@ -5,7 +5,9 @@ This file keeps track of changes in a human-readable fashion
|
|||
|
||||
These changes are not yet released
|
||||
|
||||
* Fully refactored app internally
|
||||
* Fully refactored the project internally
|
||||
* Log *directory* is now specified as opposed to *file*.
|
||||
Files in that directory are generated as `hibiscus_YYYY-MM-DD_HH:MM:SS.log`
|
||||
* Adjusted default theme
|
||||
* Error pages are now translated
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
Simple plaintext diary.
|
||||
|
||||
This project is *very* opinionated and minimal, and is designed primarily for my usage.
|
||||
As a result, I can't guarantee that it's either secure or stable.
|
||||
As a result, neither security nor stability are guaranteed.
|
||||
|
||||
## Features:
|
||||
|
||||
|
|
5
TODO.md
5
TODO.md
|
@ -1,9 +1,12 @@
|
|||
# TODO
|
||||
List of things to add to this project
|
||||
|
||||
## Urgent (1.1.5-2.0.0)
|
||||
## Priority 2.0.0
|
||||
* (pre-release) re-write README
|
||||
* `style.css` in config instead of theme (provide themes as examples in repo)
|
||||
* Auth improvement so it DOESN'T ASK ME FOR PASSWORD EVERY DAY UGH XD
|
||||
* Specify data directory in config or with flags
|
||||
* Configure more of the app with flags, possibly move to `cobra` for this
|
||||
|
||||
## Nice to have
|
||||
* Forward/backward buttons for days
|
||||
|
|
|
@ -1,12 +1,22 @@
|
|||
package config
|
||||
|
||||
type AppInfo struct {
|
||||
Version string
|
||||
SourceLink string
|
||||
version string
|
||||
source string
|
||||
}
|
||||
|
||||
// Info contains app information.
|
||||
var Info = AppInfo{
|
||||
Version: "2.0.0",
|
||||
SourceLink: "https://git.a71.su/Andrew71/hibiscus",
|
||||
version: "2.0.0",
|
||||
source: "https://git.a71.su/Andrew71/hibiscus",
|
||||
}
|
||||
|
||||
// Version returns the current app version
|
||||
func (i AppInfo) Version() string {
|
||||
return i.version
|
||||
}
|
||||
|
||||
// Source returns app's git repository
|
||||
func (i AppInfo) Source() string {
|
||||
return i.source
|
||||
}
|
|
@ -29,7 +29,7 @@ type Config struct {
|
|||
Theme string `config:"theme" type:"string"`
|
||||
Title string `config:"title" type:"string"`
|
||||
LogToFile bool `config:"log_to_file" type:"bool"`
|
||||
LogFile string `config:"log_file" type:"string"`
|
||||
LogDir string `config:"log_dir" type:"string"`
|
||||
Scram bool `config:"enable_scram" type:"bool"`
|
||||
|
||||
TelegramToken string `config:"tg_token" type:"string"`
|
||||
|
@ -47,7 +47,7 @@ var DefaultConfig = Config{
|
|||
Theme: "",
|
||||
Title: "🌺 Hibiscus.txt",
|
||||
LogToFile: false,
|
||||
LogFile: "config/log.txt",
|
||||
LogDir: "logs",
|
||||
Scram: false,
|
||||
|
||||
TelegramToken: "",
|
||||
|
|
|
@ -5,6 +5,8 @@ import (
|
|||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"git.a71.su/Andrew71/hibiscus-txt/internal/config"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
|
@ -12,19 +14,30 @@ import (
|
|||
|
||||
var DebugMode = false
|
||||
|
||||
// File returns the appropriate filename for log
|
||||
// (log_dir/hibiscus_YYYY-MM-DD_HH:MM:SS.log)
|
||||
func File() string {
|
||||
return config.Cfg.LogDir + "/hibiscus_" + time.Now().In(config.Cfg.Timezone).Format("2006-01-02_15:04:05") + ".log"
|
||||
}
|
||||
|
||||
// LogInit makes slog output to both os.Stdout and a file if needed, and sets slog.LevelDebug if enabled.
|
||||
func LogInit() {
|
||||
var w io.Writer
|
||||
logFile := File()
|
||||
var w io.Writer = os.Stdout
|
||||
if config.Cfg.LogToFile {
|
||||
f, err := os.OpenFile(config.Cfg.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
// Create dir in case it doesn't exist yet to avoid errors
|
||||
err := os.MkdirAll(path.Dir(logFile), 0755)
|
||||
if err != nil {
|
||||
slog.Error("error opening log file, logging to stdout only", "path", config.Cfg.LogFile, "error", err)
|
||||
return
|
||||
slog.Error("error creating log dir, logging to stdout only", "path", path.Dir(logFile), "error", err)
|
||||
} else {
|
||||
f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
slog.Error("error opening log file, logging to stdout only", "path", logFile, "error", err)
|
||||
return
|
||||
}
|
||||
// No defer f.Close() because that breaks the MultiWriter
|
||||
w = io.MultiWriter(f, os.Stdout)
|
||||
}
|
||||
// No defer f.Close() because that breaks the MultiWriter
|
||||
w = io.MultiWriter(f, os.Stdout)
|
||||
} else {
|
||||
w = os.Stdout
|
||||
}
|
||||
|
||||
// Make slog and chi use intended format
|
||||
|
@ -34,4 +47,5 @@ func LogInit() {
|
|||
}
|
||||
slog.SetDefault(slog.New(slog.NewTextHandler(w, opts)))
|
||||
middleware.DefaultLogger = middleware.RequestLogger(&middleware.DefaultLogFormatter{Logger: log.Default(), NoColor: true})
|
||||
slog.Debug("Debug mode enabled") // This string is only shown if debugging
|
||||
}
|
||||
|
|
|
@ -20,6 +20,6 @@ func GetInfo(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
// GetVersionApi returns current app version.
|
||||
func GetVersionApi(w http.ResponseWriter, r *http.Request) {
|
||||
HandleWrite(w.Write([]byte(config.Info.Version)))
|
||||
HandleWrite(w.Write([]byte(config.Info.Version())))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
|
|
@ -74,6 +74,5 @@ func Serve() {
|
|||
r.Handle("/public/*", fs)
|
||||
|
||||
slog.Info("🌺 Website working", "port", config.Cfg.Port)
|
||||
slog.Debug("Debug mode enabled")
|
||||
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(config.Cfg.Port), r))
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{{ define "main" }}
|
||||
<h2>{{ translate "title.info" }}</h2>
|
||||
<ul>
|
||||
<li>{{ translate "info.version" }} - {{ info.Version }} (<a href="{{ .SourceLink }}">{{ translate "info.version.link" }}</a>)</li>
|
||||
<li>{{ translate "info.version" }} - {{ info.Version }} (<a href="{{ .Source }}">{{ translate "info.version.link" }}</a>)</li>
|
||||
<li><a href="/config">{{ translate "info.config" }}</a></li>
|
||||
<li><a href="/readme">{{ translate "info.readme" }}</a></li>
|
||||
<li><a href="/api/export" download="hibiscus">{{ translate "info.export" }}</a></li>
|
||||
|
|
Loading…
Reference in a new issue