This repository was archived by the owner on Feb 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
87 lines (68 loc) · 1.63 KB
/
logger.go
File metadata and controls
87 lines (68 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Copyright (c) 2024-2025 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/
package grape
import (
"io"
"log/syslog"
"net/url"
"os"
"sync"
"go.osspkg.com/console"
"go.osspkg.com/logx"
"go.osspkg.com/grape/config"
)
var (
instance *_log = nil
mux = sync.Mutex{}
)
type _log struct {
file io.WriteCloser
handler logx.Logger
conf config.LogConfig
}
func initGlobalLogger(tag string, conf config.LogConfig, handler logx.Logger) *_log {
var err error
mux.Lock()
defer mux.Unlock()
if instance != nil {
return instance
}
instance = &_log{
conf: conf,
handler: handler,
}
switch conf.Format {
case "syslog":
network, addr := "", ""
if uri, err0 := url.Parse(conf.FilePath); err0 == nil {
network, addr = uri.Scheme, uri.Host
}
instance.file, err = syslog.Dial(network, addr, syslog.LOG_INFO, tag)
default:
instance.file, err = os.OpenFile(conf.FilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
}
console.FatalIfErr(err, "open log file: %s %s", conf.Format, conf.FilePath)
instance.handler.SetOutput(instance.file)
instance.handler.SetLevel(instance.conf.Level)
switch instance.conf.Format {
case "string", "syslog":
strFmt := logx.NewFormatString()
strFmt.SetDelimiter(' ')
instance.handler.SetFormatter(strFmt)
case "json":
instance.handler.SetFormatter(logx.NewFormatJSON())
}
return instance
}
func (v *_log) Close() error {
mux.Lock()
defer mux.Unlock()
err := v.file.Close()
instance = nil
if err != nil {
return err
}
return nil
}