Skip to content

Commit

Permalink
Merge pull request #8 for v0.5 Release
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm authored Jul 23, 2017
2 parents 336897b + cf63b41 commit a6c0702
Show file tree
Hide file tree
Showing 15 changed files with 392 additions and 232 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# log - aah framework
[![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)
[![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)
[![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)
[![License](https://img.shields.io/github/license/go-aah/log.svg)](LICENSE)

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

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

Expand Down
12 changes: 9 additions & 3 deletions console_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"runtime"

"aahframework.org/config.v0"
"aahframework.org/essentials.v0"
)

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

_ Receiver = &ConsoleReceiver{}
_ Receiver = (*ConsoleReceiver)(nil)
)

// ConsoleReceiver writes the log entry into os.Stderr.
// For non-windows it writes with color.
type ConsoleReceiver struct {
out io.Writer
formatter string
flags *[]FlagPart
flags []ess.FmtFlagPart
isCallerInfo bool
isColor bool
}
Expand All @@ -58,7 +59,7 @@ func (c *ConsoleReceiver) Init(cfg *config.Config) error {

// SetPattern method initializes the logger format pattern.
func (c *ConsoleReceiver) SetPattern(pattern string) error {
flags, err := parseFlag(pattern)
flags, err := ess.ParseFmtFlag(pattern, FmtFlags)
if err != nil {
return err
}
Expand All @@ -69,6 +70,11 @@ func (c *ConsoleReceiver) SetPattern(pattern string) error {
return nil
}

// SetWriter method sets the given writer into console receiver.
func (c *ConsoleReceiver) SetWriter(w io.Writer) {
c.out = w
}

// IsCallerInfo method returns true if log receiver is configured with caller info
// otherwise false.
func (c *ConsoleReceiver) IsCallerInfo() bool {
Expand Down
11 changes: 10 additions & 1 deletion console_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package log

import (
"io/ioutil"
"os"
"testing"

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

func TestConsoleLoggerUnknownLevel(t *testing.T) {
Expand All @@ -94,6 +96,7 @@ func TestConsoleLoggerDefaults(t *testing.T) {
logger, err := New(cfg)
assert.NotNil(t, logger)
assert.Nil(t, err)
logger.SetWriter(ioutil.Discard)

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

exit = func(code int) {}
logger.Fatal("Yes, yes, yes - at last fatal")
logger.Fatalf("Yes, yes, yes - %v", "at last fatal")
logger.Fatalln("Yes, yes, yes - %v", "at last fatal")
exit = os.Exit

assert.NotNil(t, logger.ToGoLogger())
}
45 changes: 39 additions & 6 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
slog "log"
"os"

"aahframework.org/config.v0"
)
Expand Down Expand Up @@ -92,19 +91,19 @@ func Println(format string, v ...interface{}) {
// Fatal logs message as `FATAL` and call to os.Exit(1).
func Fatal(v ...interface{}) {
std.output(levelFatal, 3, nil, v...)
os.Exit(1)
exit(1)
}

// Fatalf logs message as `FATAL` and call to os.Exit(1).
func Fatalf(format string, v ...interface{}) {
std.output(levelFatal, 3, &format, v...)
os.Exit(1)
exit(1)
}

// Fatalln logs message as `FATAL` and call to os.Exit(1).
func Fatalln(format string, v ...interface{}) {
std.output(levelFatal, 3, &format, v...)
os.Exit(1)
func Fatalln(v ...interface{}) {
std.output(levelFatal, 3, nil, v...)
exit(1)
}

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

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Logger level and assertion methods
//___________________________________

// Level method returns currently enabled logging level.
func Level() string {
return std.Level()
}

// IsLevelInfo method returns true if log level is INFO otherwise false.
func IsLevelInfo() bool {
return std.IsLevelInfo()
}

// IsLevelError method returns true if log level is ERROR otherwise false.
func IsLevelError() bool {
return std.IsLevelError()
}

// IsLevelWarn method returns true if log level is WARN otherwise false.
func IsLevelWarn() bool {
return std.IsLevelWarn()
}

// IsLevelDebug method returns true if log level is DEBUG otherwise false.
func IsLevelDebug() bool {
return std.IsLevelDebug()
}

// IsLevelTrace method returns true if log level is TRACE otherwise false.
func IsLevelTrace() bool {
return std.IsLevelTrace()
}

func init() {
cfg, _ := config.ParseString("log { }")
std, _ = New(cfg)
Expand Down
14 changes: 14 additions & 0 deletions default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package log

import (
"os"
"testing"

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

assert.Equal(t, "DEBUG", Level())
assert.True(t, IsLevelDebug())
assert.False(t, IsLevelError())
assert.False(t, IsLevelInfo())
assert.False(t, IsLevelWarn())
assert.False(t, IsLevelTrace())

exit = func(code int) {}
Fatal("fatal msg 1")
Fatalln("fatal msg %v", 2)
Fatalf("fatal msg %v", 3)
exit = os.Exit
}

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

assert.Equal(t, "DEBUG", newStd.Level())
assert.Nil(t, SetLevel("trace"))
assert.Nil(t, SetPattern("%level:-5 %message"))
}
Expand Down
43 changes: 0 additions & 43 deletions discard_receiver.go

This file was deleted.

26 changes: 13 additions & 13 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
// Entry represents a log entry and contains the timestamp when the entry
// was created, level, etc.
type Entry struct {
Level Level `json:"level,omitempty"`
Level level `json:"level,omitempty"`
Time time.Time `json:"timestamp,omitempty"`
Message string `json:"message,omitempty"`
File string `json:"file,omitempty"`
Expand Down Expand Up @@ -57,24 +57,24 @@ func (e *Entry) Reset() {
// Unexported methods
//___________________________________

// getEntry gets entry object from pool.
func getEntry() *Entry {
func acquireEntry() *Entry {
return entryPool.Get().(*Entry)
}

// putEntry reset the entry and puts it into Pool.
func putEntry(e *Entry) {
e.Reset()
entryPool.Put(e)
func releaseEntry(e *Entry) {
if e != nil {
e.Reset()
entryPool.Put(e)
}
}

// getBuffer gets buffer object from pool.
func getBuffer() *bytes.Buffer {
func acquireBuffer() *bytes.Buffer {
return bufPool.Get().(*bytes.Buffer)
}

// putBuffer reset the buffer and puts it into Pool.
func putBuffer(buf *bytes.Buffer) {
buf.Reset()
bufPool.Put(buf)
func releaseBuffer(buf *bytes.Buffer) {
if buf != nil {
buf.Reset()
bufPool.Put(buf)
}
}
Loading

0 comments on commit a6c0702

Please sign in to comment.