Skip to content

Commit 3b744de

Browse files
committed
app instance name flag and support added go-aah/aah#111
1 parent 79a40ef commit 3b744de

File tree

4 files changed

+91
-59
lines changed

4 files changed

+91
-59
lines changed

console_receiver_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestConsoleLoggerTextJSON(t *testing.T) {
3030
log {
3131
receiver = "console"
3232
level = "debug"
33-
pattern = "%time:2006-01-02 15:04:05.000 %appname %reqid %principal %level:-5 %shortfile %line %custom:- %message"
33+
pattern = "%time:2006-01-02 15:04:05.000 %appname %insname %reqid %principal %level:-5 %shortfile %line %custom:- %message"
3434
}
3535
`
3636
testConsoleLogger(t, textConfigStr2)
@@ -113,10 +113,12 @@ func testConsoleLogger(t *testing.T, cfgStr string) {
113113
logger.Trace("I shoudn't see this msg, because standard logger level is DEBUG")
114114
logger.Tracef("I shoudn't see this msg, because standard logger level is DEBUG: %v", 4)
115115

116-
logger.WithField("appname", "testlogapp").Debug("I would like to see this message, debug is useful for dev")
116+
logger.WithField("appname", "testlogapp").WithField("insname", "app-sfo-cn-01").
117+
Debug("I would like to see this message, debug is useful for dev")
117118
logger.Debugf("I would like to see this message, debug is useful for %v", "dev")
118119

119-
logger.WithField("reqid", "40139CA6368607085BF6").Info("Yes, I would love to see")
120+
logger.WithField("reqid", "40139CA6368607085BF6").WithField("insname", "app-sfo-cn-01").
121+
Info("Yes, I would love to see")
120122
logger.Infof("Yes, I would love to %v", "see")
121123

122124
logger.WithField("principal", "jeevanandam").Warn("Yes, yes it's an warning")

default_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,18 @@ func TestDefaultContextLogging(t *testing.T) {
126126

127127
func TestDefaultFieldsLogging(t *testing.T) {
128128
_ = SetPattern("%time:2006-01-02 15:04:05.000 %level:-5 %appname %reqid %principal %message %fields")
129-
WithField("appname", "value1").Info("Logging value 1")
130-
WithFields(Fields{"appname": "value1", "key1": "key1value"}).Info("Logging value 1")
129+
130+
e1 := WithFields(Fields{"appname": "value1", "key1": "value 1"})
131+
e1.Info("e1 logger")
132+
133+
e2 := e1.WithField("key2", "value 2")
134+
e2.Info("e2 logger")
135+
136+
e3 := e1.WithFields(Fields{"key3": "value 3"})
137+
e3.Info("e3 logger")
138+
139+
old := Writer()
140+
SetWriter(old)
131141
}
132142

133143
func testStdPanic(method, msg string) {

entry.go

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"encoding/json"
1010
"fmt"
11+
"strings"
1112
"sync"
1213
"time"
1314
)
@@ -24,15 +25,16 @@ type Fields map[string]interface{}
2425
// Entry represents a log entry and contains the timestamp when the entry
2526
// was created, level, etc.
2627
type Entry struct {
27-
AppName string `json:"appname,omitempty"`
28-
RequestID string `json:"reqid,omitempty"`
29-
Principal string `json:"principal,omitempty"`
30-
Level level `json:"-"`
31-
Time time.Time `json:"-"`
32-
Message string `json:"message,omitempty"`
33-
File string `json:"file,omitempty"`
34-
Line int `json:"line,omitempty"`
35-
Fields Fields `json:"fields,omitempty"`
28+
AppName string `json:"app_name,omitempty"`
29+
InstanceName string `json:"instance_name,omitempty"`
30+
RequestID string `json:"request_id,omitempty"`
31+
Principal string `json:"principal,omitempty"`
32+
Level level `json:"-"`
33+
Time time.Time `json:"-"`
34+
Message string `json:"message,omitempty"`
35+
File string `json:"file,omitempty"`
36+
Line int `json:"line,omitempty"`
37+
Fields Fields `json:"fields,omitempty"`
3638

3739
logger *Logger
3840
}
@@ -44,15 +46,22 @@ type Entry struct {
4446
// MarshalJSON method for formating entry to JSON.
4547
func (e *Entry) MarshalJSON() ([]byte, error) {
4648
type alias Entry
47-
return json.Marshal(&struct {
49+
ne := &struct {
4850
Level string `json:"level,omitempty"`
4951
Time string `json:"timestamp,omitempty"`
5052
*alias
5153
}{
52-
Level: levelToLevelName[e.Level],
54+
Level: e.Level.String(),
5355
Time: formatTime(e.Time),
5456
alias: (*alias)(e),
55-
})
57+
}
58+
59+
// delete skip fields
60+
for _, v := range strings.Fields("appname insname reqid principal") {
61+
delete(ne.Fields, v)
62+
}
63+
64+
return json.Marshal(ne)
5665
}
5766

5867
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
@@ -216,6 +225,17 @@ func (e *Entry) Reset() {
216225
e.logger = nil
217226
}
218227

228+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
229+
// Fields Unexported methods
230+
//___________________________________
231+
232+
func (f Fields) str(key string) string {
233+
if v, found := f[key]; found {
234+
return fmt.Sprint(v)
235+
}
236+
return ""
237+
}
238+
219239
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
220240
// Unexported methods
221241
//___________________________________
@@ -224,25 +244,28 @@ func (e *Entry) output(lvl level, msg string) {
224244
e.Time = time.Now()
225245
e.Level = lvl
226246
e.Message = msg
227-
e.addFields(e.logger.ctx)
247+
e.processFields()
228248
e.logger.output(e)
229249
}
230250

231251
func (e *Entry) addFields(fields Fields) {
232252
for k, v := range fields {
233-
switch k {
234-
case "appname":
235-
e.AppName = fmt.Sprint(v)
236-
case "reqid":
237-
e.RequestID = fmt.Sprint(v)
238-
case "principal":
239-
e.Principal = fmt.Sprint(v)
240-
default:
241-
e.Fields[k] = v
242-
}
253+
e.Fields[k] = v
243254
}
244255
}
245256

257+
func (e *Entry) processFields() {
258+
e.addFields(e.logger.ctx)
259+
e.AppName = e.Fields.str("appname")
260+
e.InstanceName = e.Fields.str("insname")
261+
e.RequestID = e.Fields.str("reqid")
262+
e.Principal = e.Fields.str("principal")
263+
}
264+
265+
func (e *Entry) isSkipField(key string) bool {
266+
return (key == "appname" || key == "insname" || key == "reqid" || key == "principal")
267+
}
268+
246269
func newEntry() *Entry {
247270
return &Entry{
248271
Fields: make(Fields),

formatter.go

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package log
77
import (
88
"fmt"
99
"path/filepath"
10+
"strings"
1011

1112
"aahframework.org/essentials.v0"
1213
)
@@ -15,6 +16,7 @@ import (
1516
const (
1617
FmtFlagLevel ess.FmtFlag = iota
1718
FmtFlagAppName
19+
FmtFlagInstanceName
1820
FmtFlagRequestID
1921
FmtFlagPrincipal
2022
FmtFlagTime
@@ -31,6 +33,7 @@ const (
3133
const (
3234
textFmt = "text"
3335
jsonFmt = "json"
36+
space = " "
3437
)
3538

3639
type (
@@ -58,7 +61,8 @@ var (
5861
// FmtFlags is the list of log format flags supported by aah/log library
5962
// Usage of flag order is up to format composition.
6063
// level - outputs INFO, DEBUG, ERROR, so on
61-
// appname - outputs Application Name (aka instance name)
64+
// appname - outputs Application Name
65+
// insname - outputs Application Instance Name
6266
// reqid - outputs Request ID from HTTP header
6367
// principal - outputs Logged-In subject primary principal
6468
// level - outputs INFO, DEBUG, ERROR, so on
@@ -73,6 +77,7 @@ var (
7377
FmtFlags = map[string]ess.FmtFlag{
7478
"level": FmtFlagLevel,
7579
"appname": FmtFlagAppName,
80+
"insname": FmtFlagInstanceName,
7681
"reqid": FmtFlagRequestID,
7782
"principal": FmtFlagPrincipal,
7883
"time": FmtFlagTime,
@@ -100,56 +105,48 @@ func textFormatter(flags []ess.FmtFlagPart, entry *Entry) []byte {
100105
for _, part := range flags {
101106
switch part.Flag {
102107
case FmtFlagLevel:
103-
buf.WriteString(fmt.Sprintf(part.Format, entry.Level))
104-
buf.WriteByte(' ')
108+
buf.WriteString(fmt.Sprintf(part.Format, entry.Level) + space)
105109
case FmtFlagAppName:
106110
if len(entry.AppName) > 0 {
107-
buf.WriteString(entry.AppName)
108-
buf.WriteByte(' ')
111+
buf.WriteString(entry.AppName + space)
112+
}
113+
case FmtFlagInstanceName:
114+
if len(entry.InstanceName) > 0 {
115+
buf.WriteString(entry.InstanceName + space)
109116
}
110117
case FmtFlagRequestID:
111118
if len(entry.RequestID) > 0 {
112-
buf.WriteString(entry.RequestID)
113-
buf.WriteByte(' ')
119+
buf.WriteString(entry.RequestID + space)
114120
}
115121
case FmtFlagPrincipal:
116122
if len(entry.Principal) > 0 {
117-
buf.WriteString(entry.Principal)
118-
buf.WriteByte(' ')
123+
buf.WriteString(entry.Principal + space)
119124
}
120125
case FmtFlagTime:
121-
buf.WriteString(entry.Time.Format(part.Format))
122-
buf.WriteByte(' ')
126+
buf.WriteString(entry.Time.Format(part.Format) + space)
123127
case FmtFlagUTCTime:
124-
buf.WriteString(entry.Time.UTC().Format(part.Format))
125-
buf.WriteByte(' ')
128+
buf.WriteString(entry.Time.UTC().Format(part.Format) + space)
126129
case FmtFlagLongfile, FmtFlagShortfile:
127130
if part.Flag == FmtFlagShortfile {
128131
entry.File = filepath.Base(entry.File)
129132
}
130-
buf.WriteString(fmt.Sprintf(part.Format, entry.File))
131-
buf.WriteByte(' ')
133+
buf.WriteString(fmt.Sprintf(part.Format, entry.File) + space)
132134
case FmtFlagLine:
133-
buf.WriteString("L" + fmt.Sprintf(part.Format, entry.Line))
134-
buf.WriteByte(' ')
135+
buf.WriteString("L" + fmt.Sprintf(part.Format, entry.Line) + space)
135136
case FmtFlagMessage:
136-
buf.WriteString(entry.Message)
137-
buf.WriteByte(' ')
137+
buf.WriteString(entry.Message + space)
138138
case FmtFlagCustom:
139-
buf.WriteString(part.Format)
140-
buf.WriteByte(' ')
139+
buf.WriteString(part.Format + space)
141140
case FmtFlagFields:
142-
if cnt := len(entry.Fields); cnt > 0 {
143-
buf.WriteString("fields[")
144-
for k, v := range entry.Fields {
145-
cnt--
146-
buf.WriteString(fmt.Sprintf("%v: %v", k, v))
147-
if cnt != 0 {
148-
buf.WriteString(", ")
149-
}
141+
fs := make([]string, 0)
142+
for k, v := range entry.Fields {
143+
if !entry.isSkipField(k) {
144+
fs = append(fs, fmt.Sprintf("%v: %v", k, v))
150145
}
151-
buf.WriteString("]")
152-
buf.WriteByte(' ')
146+
}
147+
148+
if len(fs) > 0 {
149+
buf.WriteString("fields[" + strings.Join(fs, ", ") + "] ")
153150
}
154151
}
155152
}

0 commit comments

Comments
 (0)