Permalink
Browse files

Take map of interface{} as data argument

  • Loading branch information...
1 parent 76b0840 commit 273887d19cbb57225934ac7cd058bd75b14c4f2a Dmitriy Kalinin and Pieter Noordhuis committed Mar 7, 2013
Showing with 47 additions and 45 deletions.
  1. +1 −1 io_sink_test.go
  2. +1 −1 json_codec_test.go
  3. +1 −1 json_prettifier.go
  4. +4 −4 json_prettifier_test.go
  5. +4 −3 logger.go
  6. +1 −1 logger_test.go
  7. +1 −1 perf_test.go
  8. +10 −10 record.go
  9. +3 −3 record_test.go
  10. +17 −16 tagged_logger.go
  11. +4 −4 tagged_logger_test.go
View
@@ -19,7 +19,7 @@ func (s *IOSinkSuite) TestAddRecord(c *C) {
sink.SetCodec(NewJsonCodec())
go func(msg string) {
- record := NewRecord("source", LOG_INFO, msg, map[string]string{})
+ record := NewRecord("source", LOG_INFO, msg, nil)
sink.AddRecord(record)
sink.Flush()
pWriter.Close()
View
@@ -12,7 +12,7 @@ type JsonCodecSuite struct {
var _ = Suite(&JsonCodecSuite{})
func (s *JsonCodecSuite) TestJsonCodec(c *C) {
- r := NewRecord("source", LOG_INFO, "Hello world", map[string]string{})
+ r := NewRecord("source", LOG_INFO, "Hello world", nil)
m, err := s.EncodeRecord(r)
c.Assert(err, IsNil)
View
@@ -95,7 +95,7 @@ func encodeMethod(method string) string {
return fmt.Sprintf("%s ", method[index+1:])
}
-func encodeData(data map[string]string) (string, error) {
+func encodeData(data map[string]interface{}) (string, error) {
b, err := json.Marshal(data)
return fmt.Sprintf("%s ", string(b)), err
}
View
@@ -18,7 +18,7 @@ func (s *JsonPrettifierSuite) TestConst(c *C) {
}
func (s *JsonPrettifierSuite) TestConstOrder(c *C) {
- record := NewRecord("source", LOG_INFO, "Hello, world", map[string]string{"foo": "bar"})
+ record := NewRecord("source", LOG_INFO, "Hello, world", map[string]interface{}{"foo": "bar"})
prettifier1 := NewJsonPrettifier(EXCLUDE_FILE | EXCLUDE_DATA)
bytes1, _ := prettifier1.EncodeRecord(record)
@@ -31,7 +31,7 @@ func (s *JsonPrettifierSuite) TestConstOrder(c *C) {
func (s *JsonPrettifierSuite) TestEncodeRecord(c *C) {
config.EnableLOC = true
- record := NewRecord("source", LOG_INFO, "Hello, world", map[string]string{"foo": "bar"})
+ record := NewRecord("source", LOG_INFO, "Hello, world", map[string]interface{}{"foo": "bar"})
config.EnableLOC = false
l := record.Line
@@ -46,7 +46,7 @@ func (s *JsonPrettifierSuite) TestEncodeRecord(c *C) {
func (s *JsonPrettifierSuite) TestExclude(c *C) {
config.EnableLOC = true
- record := NewRecord("source", LOG_INFO, "Hello, world", map[string]string{"foo": "bar"})
+ record := NewRecord("source", LOG_INFO, "Hello, world", map[string]interface{}{"foo": "bar"})
config.EnableLOC = false
prettifier := NewJsonPrettifier(EXCLUDE_DATA | EXCLUDE_LINE)
@@ -59,7 +59,7 @@ func (s *JsonPrettifierSuite) TestExclude(c *C) {
func (s *JsonPrettifierSuite) TestDecodeLogEntry(c *C) {
config.EnableLOC = true
- record := NewRecord("source", LOG_INFO, "Hello, world", map[string]string{"foo": "bar"})
+ record := NewRecord("source", LOG_INFO, "Hello, world", map[string]interface{}{"foo": "bar"})
config.EnableLOC = false
l := record.Line
t := record.Timestamp
View
@@ -9,7 +9,8 @@ import (
type Logger interface {
json.Marshaler
- Log(level LogLevel, m string, data map[string]string)
+ Log(level LogLevel, m string, d map[string]interface{})
+
Fatal(m string)
Error(m string)
Warn(m string)
@@ -33,12 +34,12 @@ type BaseLogger struct {
level LogLevel
}
-func (x *BaseLogger) Log(l LogLevel, m string, data map[string]string) {
+func (x *BaseLogger) Log(l LogLevel, m string, d map[string]interface{}) {
if !x.active(l) {
return
}
- record := NewRecord(x.name, l, m, data)
+ record := NewRecord(x.name, l, m, d)
for _, sink := range x.sinks {
sink.AddRecord(record)
View
@@ -66,7 +66,7 @@ func (s *LoggerSuite) TestPanic(c *C) {
c.Assert(func() { logger.Fatal("fail!") }, PanicMatches, "fail!")
c.Assert(func() { logger.Fatalf("fail!%s", "fail!") }, PanicMatches, "fail!fail!")
- t := NewTaggedLogger(logger, map[string]string{"foo": "bar"})
+ t := NewTaggedLogger(logger, map[string]interface{}{"foo": "bar"})
c.Assert(func() { t.Fatal("panic") }, PanicMatches, "panic")
c.Assert(func() { t.Fatalf("panic!%s", "panic!") }, PanicMatches, "panic!panic!")
}
View
@@ -38,7 +38,7 @@ func BenchmarkTaggedLoggerInDevNullSink(b *testing.B) {
Sinks: []Sink{NewFileSink("/dev/null")},
})
loggers = make(map[string]*BaseLogger)
- tags := map[string]string{
+ tags := map[string]interface{}{
"thread_id": "1234",
"program_name": "benchmark",
}
View
@@ -8,15 +8,15 @@ import (
)
type Record struct {
- Timestamp float64 `json:"timestamp"`
- Pid int `json:"process_id"`
- Source string `json:"source"`
- Level LogLevel `json:"log_level"`
- Message string `json:"message"`
- Data map[string]string `json:"data"`
- File string `json:"file"`
- Line int `json:"line"`
- Method string `json:"method"`
+ Timestamp float64 `json:"timestamp"`
+ Pid int `json:"process_id"`
+ Source string `json:"source"`
+ Level LogLevel `json:"log_level"`
+ Message string `json:"message"`
+ Data map[string]interface{} `json:"data"`
+ File string `json:"file"`
+ Line int `json:"line"`
+ Method string `json:"method"`
}
var pid int
@@ -25,7 +25,7 @@ func init() {
pid = os.Getpid()
}
-func NewRecord(s string, l LogLevel, m string, d map[string]string) *Record {
+func NewRecord(s string, l LogLevel, m string, d map[string]interface{}) *Record {
r := &Record{
Timestamp: float64(time.Now().UnixNano()) / 1000000000,
Pid: pid,
View
@@ -10,7 +10,7 @@ var _ = Suite(&RecordSuite{})
func (s *RecordSuite) TestNewRecordWithLOC(c *C) {
config.EnableLOC = true
- r := NewRecord("source", LOG_INFO, "hello", map[string]string{})
+ r := NewRecord("source", LOG_INFO, "hello", nil)
config.EnableLOC = false
c.Check(r.File, Matches, ".*record_test.go$")
@@ -19,15 +19,15 @@ func (s *RecordSuite) TestNewRecordWithLOC(c *C) {
}
func (s *RecordSuite) TestNewRecordWithoutLOC(c *C) {
- r := NewRecord("source", LOG_INFO, "hello", map[string]string{})
+ r := NewRecord("source", LOG_INFO, "hello", nil)
c.Check(r.File, Equals, "")
c.Check(r.Line, Equals, 0)
c.Check(r.Method, Equals, "")
}
func (s *RecordSuite) TestRecordPid(c *C) {
- r := NewRecord("source", LOG_INFO, "hello", map[string]string{})
+ r := NewRecord("source", LOG_INFO, "hello", nil)
c.Check(r.Pid, Not(Equals), 0)
}
View
@@ -7,34 +7,35 @@ import (
type TaggedLogger struct {
proxyLogger Logger
- data map[string]string
+ d map[string]interface{}
}
-// tagged logger doesn't have name, so far
-func NewTaggedLogger(logger Logger, data map[string]string) Logger {
+func NewTaggedLogger(l Logger, d map[string]interface{}) Logger {
taggedLogger := new(TaggedLogger)
- taggedLogger.proxyLogger = logger
- taggedLogger.data = data
+ taggedLogger.proxyLogger = l
+ taggedLogger.d = d
return taggedLogger
}
-func (x *TaggedLogger) Log(l LogLevel, m string, data map[string]string) {
- if data != nil {
- d := make(map[string]string)
+func (x *TaggedLogger) Log(l LogLevel, m string, d map[string]interface{}) {
+ if d != nil {
+ e := make(map[string]interface{})
- // data will cover userData if key is the same
- for k, v := range x.data {
- d[k] = v
+ // Copy the logger's data
+ for k, v := range x.d {
+ e[k] = v
}
- for k, v := range data {
- d[k] = v
+
+ // Overwrite specified data
+ for k, v := range d {
+ e[k] = v
}
- x.proxyLogger.Log(l, m, d)
+ x.proxyLogger.Log(l, m, e)
} else {
- x.proxyLogger.Log(l, m, x.data)
+ x.proxyLogger.Log(l, m, x.d)
}
}
@@ -96,7 +97,7 @@ func (x *TaggedLogger) Debug2f(f string, a ...interface{}) {
}
func (x *TaggedLogger) MarshalJSON() ([]byte, error) {
- data, _ := json.Marshal(x.data)
+ data, _ := json.Marshal(x.d)
proxy, _ := json.Marshal(x.proxyLogger)
msg := fmt.Sprintf("{\"data\": %s, \"proxy\": %s}", data, proxy)
View
@@ -30,7 +30,7 @@ func (s *TaggedLoggerSuite) TearDownTest(c *C) {
func (s *TaggedLoggerSuite) TestNewTaggedLogger(c *C) {
logger := NewLogger("foobar")
- taggedLogger := NewTaggedLogger(logger, map[string]string{"foo": "bar"})
+ taggedLogger := NewTaggedLogger(logger, map[string]interface{}{"foo": "bar"})
taggedLogger.Info("Hello")
taggedLogger.Debug("World")
// the level of tagged logger should be the same as the derived logger
@@ -39,16 +39,16 @@ func (s *TaggedLoggerSuite) TestNewTaggedLogger(c *C) {
func (s *TaggedLoggerSuite) TestTaggedLogger(c *C) {
logger := NewLogger("foobar")
- taggedLogger := NewTaggedLogger(logger, map[string]string{"foo": "bar"})
+ taggedLogger := NewTaggedLogger(logger, map[string]interface{}{"foo": "bar"})
taggedLogger.Info("Hello")
bytes, _ := config.Codec.EncodeRecord(s.nSink.records[0])
c.Assert(string(bytes), Matches, `{.*"foo":"bar".*}`)
}
func (s *TaggedLoggerSuite) TestTaggedLogger2(c *C) {
logger := NewLogger("whatever")
- taggedLogger := NewTaggedLogger(logger, map[string]string{"foo": "bar"})
- taggedLogger2 := NewTaggedLogger(taggedLogger, map[string]string{"oof": "rab"})
+ taggedLogger := NewTaggedLogger(logger, map[string]interface{}{"foo": "bar"})
+ taggedLogger2 := NewTaggedLogger(taggedLogger, map[string]interface{}{"oof": "rab"})
taggedLogger2.Info("Hello")
c.Assert(s.nSink.records, HasLen, 1)

0 comments on commit 273887d

Please sign in to comment.