Skip to content

Commit 49dc031

Browse files
committed
New options add.
1 parent 37719d8 commit 49dc031

3 files changed

Lines changed: 111 additions & 24 deletions

File tree

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,21 @@ go get github.com/kayu0514/VersaLog.go
2121

2222
### Options
2323

24-
| Options | Description |
25-
| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
26-
| `show_file` | True : Display filename and line number (for simple and detailed modes) |
27-
| `show_tag` | True : Show self.tag if no explicit tag is provided |
28-
| `tag` | Default tag to use when show_tag is enabled |
29-
| `enable_all` | Shortcut to enable both show_file and show_tag |
30-
| `notice` | True : When an error or critical level log is output, a desktop notification (using plyer.notification) will be displayed. The notification includes the log level and message. |
24+
| Options | Description |
25+
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
26+
| `show_file` | True : Display filename and line number (for simple and detailed modes) |
27+
| `show_tag` | True : Show self.tag if no explicit tag is provided |
28+
| `tag` | Default tag to use when show_tag is enabled |
29+
| `enable_all` | Shortcut to enable both show_file and show_tag |
30+
| `notice` | True : When an error or critical level log is output, a desktop notification (using plyer.notification) will be displayed. The notification includes the log level and message. |
31+
| `all_save` | True : When an error or critical level log is output, the log will be saved to a file. |
32+
| `save_levels` | A list of log levels to save. Defaults to ["INFO", "ERROR", "WARNING", "DEBUG", "CRITICAL"]. |
33+
34+
## Log save
35+
36+
```
37+
[2025-08-06 04:10:36][INFO] : ok
38+
```
3139

3240
## Sample
3341

VersaLog/versalog.go

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package versalog
22

33
import (
44
"fmt"
5+
"os"
56
"path/filepath"
67
"runtime"
78
"strings"
@@ -11,12 +12,14 @@ import (
1112
)
1213

1314
type VersaLog struct {
14-
mode string
15-
tag string
16-
showFile bool
17-
showtag bool
18-
Notice bool
19-
EnableAll bool
15+
mode string
16+
tag string
17+
showFile bool
18+
showTag bool
19+
Notice bool
20+
EnableAll bool
21+
AllSave bool
22+
SaveLevels []string
2023
}
2124

2225
var COLORS = map[string]string{
@@ -37,31 +40,54 @@ var SYMBOLS = map[string]string{
3740

3841
const RESET = "\033[0m"
3942

40-
func NewVersaLog(mode string, showFile bool, showtag bool, tag string, enableAll bool, notice bool) *VersaLog {
43+
func NewVersaLog(mode string, showFile bool, showTag bool, tag string, enableAll bool, notice bool, allSave bool, saveLevels []string) *VersaLog {
4144
mode = strings.ToLower(mode)
4245

4346
validModes := map[string]bool{"simple": true, "simple2": true, "detailed": true, "file": true}
4447
if !validModes[mode] {
45-
panic(fmt.Sprintf("Invalid mode '%s' specified. Valid modes are: simple, detailed, file", mode))
48+
panic(fmt.Sprintf("Invalid mode '%s' specified. Valid modes are: simple, simple2, detailed, file", mode))
4649
}
4750

4851
if enableAll {
4952
showFile = true
50-
showtag = true
53+
showTag = true
5154
notice = true
55+
allSave = true
5256
}
5357

5458
if mode == "file" {
5559
showFile = true
5660
}
5761

62+
validSaveLevels := []string{"INFO", "ERROR", "WARNING", "DEBUG", "CRITICAL"}
63+
if allSave {
64+
if len(saveLevels) == 0 {
65+
saveLevels = append([]string{}, validSaveLevels...)
66+
} else {
67+
for _, l := range saveLevels {
68+
found := false
69+
for _, v := range validSaveLevels {
70+
if l == v {
71+
found = true
72+
break
73+
}
74+
}
75+
if !found {
76+
panic(fmt.Sprintf("Invalid saveLevels specified. Valid levels are: %v", validSaveLevels))
77+
}
78+
}
79+
}
80+
}
81+
5882
return &VersaLog{
59-
mode: mode,
60-
showFile: showFile,
61-
showtag: showtag,
62-
tag: tag,
63-
Notice: notice,
64-
EnableAll: enableAll,
83+
mode: mode,
84+
showFile: showFile,
85+
showTag: showTag,
86+
tag: tag,
87+
Notice: notice,
88+
EnableAll: enableAll,
89+
AllSave: allSave,
90+
SaveLevels: saveLevels,
6591
}
6692
}
6793

@@ -77,6 +103,33 @@ func (v *VersaLog) getCaller() string {
77103
return fmt.Sprintf("%s:%d", filepath.Base(file), line)
78104
}
79105

106+
func (v *VersaLog) saveLog(logText string, level string) {
107+
if !v.AllSave {
108+
return
109+
}
110+
found := false
111+
for _, l := range v.SaveLevels {
112+
if l == level {
113+
found = true
114+
break
115+
}
116+
}
117+
if !found {
118+
return
119+
}
120+
logDir := filepath.Join(filepath.Dir(filepath.Dir("./VersaLog/versalog.go")), "log")
121+
if _, err := os.Stat(logDir); os.IsNotExist(err) {
122+
os.MkdirAll(logDir, 0755)
123+
}
124+
logFile := filepath.Join(logDir, time.Now().Format("2006-01-02")+".log")
125+
f, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
126+
if err != nil {
127+
return
128+
}
129+
defer f.Close()
130+
f.WriteString(logText + "\n")
131+
}
132+
80133
func (v *VersaLog) log(msg string, level string, tag ...string) {
81134
level = strings.ToUpper(level)
82135
color := COLORS[level]
@@ -86,56 +139,69 @@ func (v *VersaLog) log(msg string, level string, tag ...string) {
86139

87140
if len(tag) > 0 && tag[0] != "" {
88141
finalTag = tag[0]
89-
} else if v.showtag && v.tag != "" {
142+
} else if v.showTag && v.tag != "" {
90143
finalTag = v.tag
91144
}
92145

93146
if v.showFile || v.mode == "file" {
94147
caller = v.getCaller()
95148
}
96149

97-
var output string
150+
var output, plain string
98151
switch v.mode {
99152
case "simple":
100153
if v.showFile {
101154
if finalTag != "" {
102155
output = fmt.Sprintf("[%s][%s]%s%s%s %s", caller, finalTag, color, symbol, RESET, msg)
156+
plain = fmt.Sprintf("[%s][%s]%s %s", caller, finalTag, symbol, msg)
103157
} else {
104158
output = fmt.Sprintf("[%s]%s%s%s %s", caller, color, symbol, RESET, msg)
159+
plain = fmt.Sprintf("[%s]%s %s", caller, symbol, msg)
105160
}
106161
} else {
107162
if finalTag != "" {
108163
output = fmt.Sprintf("[%s]%s%s%s %s", finalTag, color, symbol, RESET, msg)
164+
plain = fmt.Sprintf("[%s]%s %s", finalTag, symbol, msg)
109165
} else {
110166
output = fmt.Sprintf("%s%s%s %s", color, symbol, RESET, msg)
167+
plain = fmt.Sprintf("%s %s", symbol, msg)
111168
}
112169
}
113170
case "simple2":
114171
timestamp := v.getTime()
115172
if v.showFile {
116173
if finalTag != "" {
117174
output = fmt.Sprintf("[%s] [%s][%s]%s%s%s %s", timestamp, caller, finalTag, color, symbol, RESET, msg)
175+
plain = fmt.Sprintf("[%s] [%s][%s]%s %s", timestamp, caller, finalTag, symbol, msg)
118176
} else {
119177
output = fmt.Sprintf("[%s] [%s]%s%s%s %s", timestamp, caller, color, symbol, RESET, msg)
178+
plain = fmt.Sprintf("[%s] [%s]%s %s", timestamp, caller, symbol, msg)
120179
}
121180
} else {
122181
output = fmt.Sprintf("[%s] %s%s%s %s", timestamp, color, symbol, RESET, msg)
182+
plain = fmt.Sprintf("[%s] %s %s", timestamp, symbol, msg)
123183
}
124184
case "file":
125185
output = fmt.Sprintf("[%s]%s[%s]%s %s", caller, color, level, RESET, msg)
186+
plain = fmt.Sprintf("[%s][%s] %s", caller, level, msg)
126187
default:
127188
timestamp := v.getTime()
128189
output = fmt.Sprintf("[%s]%s[%s]%s", timestamp, color, level, RESET)
190+
plain = fmt.Sprintf("[%s][%s]", timestamp, level)
129191
if finalTag != "" {
130192
output += fmt.Sprintf("[%s]", finalTag)
193+
plain += fmt.Sprintf("[%s]", finalTag)
131194
}
132195
if v.showFile {
133196
output += fmt.Sprintf("[%s]", caller)
197+
plain += fmt.Sprintf("[%s]", caller)
134198
}
135199
output += fmt.Sprintf(" : %s", msg)
200+
plain += fmt.Sprintf(" : %s", msg)
136201
}
137202

138203
fmt.Println(output)
204+
v.saveLog(plain, level)
139205

140206
if v.Notice && (level == "ERROR" || level == "CRITICAL") {
141207
toastMessage := toast.Notification{

tests/log_save_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import versalog "github.com/kayu0514/VersaLog.go/VersaLog"
4+
5+
func main() {
6+
logger := versalog.NewVersaLog("detailed", false, false, "VersaLog", false, false, true)
7+
8+
logger.Info("info")
9+
logger.Error("error")
10+
logger.Warning("warning.")
11+
logger.Debug("debug")
12+
logger.Critical("critical")
13+
}

0 commit comments

Comments
 (0)