Skip to content

Commit a6c0702

Browse files
authored
Merge pull request #8 for v0.5 Release
2 parents 336897b + cf63b41 commit a6c0702

15 files changed

+392
-232
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# log - aah framework
22
[![Build Status](https://travis-ci.org/go-aah/log.svg?branch=master)](https://travis-ci.org/go-aah/log) [![codecov](https://codecov.io/gh/go-aah/log/branch/master/graph/badge.svg)](https://codecov.io/gh/go-aah/log/branch/master) [![Go Report Card](https://goreportcard.com/badge/aahframework.org/log.v0)](https://goreportcard.com/report/aahframework.org/log.v0)
3-
[![Version](https://img.shields.io/badge/version-0.4-blue.svg)](https://github.com/go-aah/log/releases/latest) [![GoDoc](https://godoc.org/aahframework.org/log.v0?status.svg)](https://godoc.org/aahframework.org/log.v0)
3+
[![Version](https://img.shields.io/badge/version-0.5-blue.svg)](https://github.com/go-aah/log/releases/latest) [![GoDoc](https://godoc.org/aahframework.org/log.v0?status.svg)](https://godoc.org/aahframework.org/log.v0)
44
[![License](https://img.shields.io/github/license/go-aah/log.svg)](LICENSE)
55

6-
***v0.4 [released](https://github.com/go-aah/log/releases/latest) and tagged on Jun 01, 2017***
6+
***v0.5 [released](https://github.com/go-aah/log/releases/latest) and tagged on Jul 22, 2017***
77

88
Simple, flexible & powerful `Go` logger inspired by standard logger & Google glog. aah framework utilizes `log` library across.
99

console_receiver.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"runtime"
1212

1313
"aahframework.org/config.v0"
14+
"aahframework.org/essentials.v0"
1415
)
1516

1617
var (
@@ -26,15 +27,15 @@ var (
2627
LevelTrace: []byte("\033[0;35m"), // magenta (purple)
2728
}
2829

29-
_ Receiver = &ConsoleReceiver{}
30+
_ Receiver = (*ConsoleReceiver)(nil)
3031
)
3132

3233
// ConsoleReceiver writes the log entry into os.Stderr.
3334
// For non-windows it writes with color.
3435
type ConsoleReceiver struct {
3536
out io.Writer
3637
formatter string
37-
flags *[]FlagPart
38+
flags []ess.FmtFlagPart
3839
isCallerInfo bool
3940
isColor bool
4041
}
@@ -58,7 +59,7 @@ func (c *ConsoleReceiver) Init(cfg *config.Config) error {
5859

5960
// SetPattern method initializes the logger format pattern.
6061
func (c *ConsoleReceiver) SetPattern(pattern string) error {
61-
flags, err := parseFlag(pattern)
62+
flags, err := ess.ParseFmtFlag(pattern, FmtFlags)
6263
if err != nil {
6364
return err
6465
}
@@ -69,6 +70,11 @@ func (c *ConsoleReceiver) SetPattern(pattern string) error {
6970
return nil
7071
}
7172

73+
// SetWriter method sets the given writer into console receiver.
74+
func (c *ConsoleReceiver) SetWriter(w io.Writer) {
75+
c.out = w
76+
}
77+
7278
// IsCallerInfo method returns true if log receiver is configured with caller info
7379
// otherwise false.
7480
func (c *ConsoleReceiver) IsCallerInfo() bool {

console_receiver_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package log
66

77
import (
8+
"io/ioutil"
9+
"os"
810
"testing"
911

1012
"aahframework.org/config.v0"
@@ -67,7 +69,7 @@ func TestConsoleLoggerUnknownFormatFlag(t *testing.T) {
6769
cfg, _ := config.ParseString(configStr)
6870
logger, err := New(cfg)
6971
assert.Nil(t, logger)
70-
assert.Equal(t, "unrecognized log format flag: myfile", err.Error())
72+
assert.Equal(t, "fmtflag: unknown flag 'myfile'", err.Error())
7173
}
7274

7375
func TestConsoleLoggerUnknownLevel(t *testing.T) {
@@ -94,6 +96,7 @@ func TestConsoleLoggerDefaults(t *testing.T) {
9496
logger, err := New(cfg)
9597
assert.NotNil(t, logger)
9698
assert.Nil(t, err)
99+
logger.SetWriter(ioutil.Discard)
97100

98101
// receiver nil scenario
99102
logger.receiver = nil
@@ -121,5 +124,11 @@ func testConsoleLogger(t *testing.T, cfgStr string) {
121124
logger.Error("Yes, yes, yes - finally an error")
122125
logger.Errorf("Yes, yes, yes - %v", "finally an error")
123126

127+
exit = func(code int) {}
128+
logger.Fatal("Yes, yes, yes - at last fatal")
129+
logger.Fatalf("Yes, yes, yes - %v", "at last fatal")
130+
logger.Fatalln("Yes, yes, yes - %v", "at last fatal")
131+
exit = os.Exit
132+
124133
assert.NotNil(t, logger.ToGoLogger())
125134
}

default.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"io"
1010
slog "log"
11-
"os"
1211

1312
"aahframework.org/config.v0"
1413
)
@@ -92,19 +91,19 @@ func Println(format string, v ...interface{}) {
9291
// Fatal logs message as `FATAL` and call to os.Exit(1).
9392
func Fatal(v ...interface{}) {
9493
std.output(levelFatal, 3, nil, v...)
95-
os.Exit(1)
94+
exit(1)
9695
}
9796

9897
// Fatalf logs message as `FATAL` and call to os.Exit(1).
9998
func Fatalf(format string, v ...interface{}) {
10099
std.output(levelFatal, 3, &format, v...)
101-
os.Exit(1)
100+
exit(1)
102101
}
103102

104103
// Fatalln logs message as `FATAL` and call to os.Exit(1).
105-
func Fatalln(format string, v ...interface{}) {
106-
std.output(levelFatal, 3, &format, v...)
107-
os.Exit(1)
104+
func Fatalln(v ...interface{}) {
105+
std.output(levelFatal, 3, nil, v...)
106+
exit(1)
108107
}
109108

110109
// Panic logs message as `PANIC` and call to panic().
@@ -150,6 +149,40 @@ func SetPattern(pattern string) error {
150149
return std.SetPattern(pattern)
151150
}
152151

152+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
153+
// Logger level and assertion methods
154+
//___________________________________
155+
156+
// Level method returns currently enabled logging level.
157+
func Level() string {
158+
return std.Level()
159+
}
160+
161+
// IsLevelInfo method returns true if log level is INFO otherwise false.
162+
func IsLevelInfo() bool {
163+
return std.IsLevelInfo()
164+
}
165+
166+
// IsLevelError method returns true if log level is ERROR otherwise false.
167+
func IsLevelError() bool {
168+
return std.IsLevelError()
169+
}
170+
171+
// IsLevelWarn method returns true if log level is WARN otherwise false.
172+
func IsLevelWarn() bool {
173+
return std.IsLevelWarn()
174+
}
175+
176+
// IsLevelDebug method returns true if log level is DEBUG otherwise false.
177+
func IsLevelDebug() bool {
178+
return std.IsLevelDebug()
179+
}
180+
181+
// IsLevelTrace method returns true if log level is TRACE otherwise false.
182+
func IsLevelTrace() bool {
183+
return std.IsLevelTrace()
184+
}
185+
153186
func init() {
154187
cfg, _ := config.ParseString("log { }")
155188
std, _ = New(cfg)

default_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package log
66

77
import (
8+
"os"
89
"testing"
910

1011
"aahframework.org/config.v0"
@@ -42,6 +43,18 @@ func TestDefaultLogger(t *testing.T) {
4243
testStdPanic("panicf", "this is panicf")
4344
testStdPanic("panicln", "this is panicln")
4445

46+
assert.Equal(t, "DEBUG", Level())
47+
assert.True(t, IsLevelDebug())
48+
assert.False(t, IsLevelError())
49+
assert.False(t, IsLevelInfo())
50+
assert.False(t, IsLevelWarn())
51+
assert.False(t, IsLevelTrace())
52+
53+
exit = func(code int) {}
54+
Fatal("fatal msg 1")
55+
Fatalln("fatal msg %v", 2)
56+
Fatalf("fatal msg %v", 3)
57+
exit = os.Exit
4558
}
4659

4760
func TestDefaultLoggerMisc(t *testing.T) {
@@ -52,6 +65,7 @@ func TestDefaultLoggerMisc(t *testing.T) {
5265
Printf("welcome 2 printf")
5366
Println("welcome 2 println")
5467

68+
assert.Equal(t, "DEBUG", newStd.Level())
5569
assert.Nil(t, SetLevel("trace"))
5670
assert.Nil(t, SetPattern("%level:-5 %message"))
5771
}

discard_receiver.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

entry.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var (
1919
// Entry represents a log entry and contains the timestamp when the entry
2020
// was created, level, etc.
2121
type Entry struct {
22-
Level Level `json:"level,omitempty"`
22+
Level level `json:"level,omitempty"`
2323
Time time.Time `json:"timestamp,omitempty"`
2424
Message string `json:"message,omitempty"`
2525
File string `json:"file,omitempty"`
@@ -57,24 +57,24 @@ func (e *Entry) Reset() {
5757
// Unexported methods
5858
//___________________________________
5959

60-
// getEntry gets entry object from pool.
61-
func getEntry() *Entry {
60+
func acquireEntry() *Entry {
6261
return entryPool.Get().(*Entry)
6362
}
6463

65-
// putEntry reset the entry and puts it into Pool.
66-
func putEntry(e *Entry) {
67-
e.Reset()
68-
entryPool.Put(e)
64+
func releaseEntry(e *Entry) {
65+
if e != nil {
66+
e.Reset()
67+
entryPool.Put(e)
68+
}
6969
}
7070

71-
// getBuffer gets buffer object from pool.
72-
func getBuffer() *bytes.Buffer {
71+
func acquireBuffer() *bytes.Buffer {
7372
return bufPool.Get().(*bytes.Buffer)
7473
}
7574

76-
// putBuffer reset the buffer and puts it into Pool.
77-
func putBuffer(buf *bytes.Buffer) {
78-
buf.Reset()
79-
bufPool.Put(buf)
75+
func releaseBuffer(buf *bytes.Buffer) {
76+
if buf != nil {
77+
buf.Reset()
78+
bufPool.Put(buf)
79+
}
8080
}

0 commit comments

Comments
 (0)