This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
379 additions
and
555 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,46 @@ | ||
go-log - Simple logging library | ||
=============================== | ||
log - Simple logging library | ||
============================ | ||
|
||
[![wercker status](https://app.wercker.com/status/c98a6d8f01bffef5bca40c3563347dba/m "wercker status")](https://app.wercker.com/project/bykey/c98a6d8f01bffef5bca40c3563347dba) | ||
[![wercker status](https://app.wercker.com/status/054f2a1e2351df9bc41d07889f2bcf36/s/master "wercker status")](https://app.wercker.com/project/bykey/054f2a1e2351df9bc41d07889f2bcf36) | ||
[![Coverage Status](https://coveralls.io/repos/zhevron/log/badge.svg?branch=master&service=github)](https://coveralls.io/github/zhevron/log?branch=master) | ||
[![GoDoc](https://godoc.org/github.com/zhevron/log?status.svg)](https://godoc.org/github.com/zhevron/log) | ||
|
||
[![Coverage Status](https://coveralls.io/repos/zhevron/go-log/badge.svg?branch=HEAD)](https://coveralls.io/r/zhevron/go-log?branch=HEAD) | ||
[![GoDoc](https://godoc.org/gopkg.in/zhevron/go-log.v0/log?status.svg)](https://godoc.org/gopkg.in/zhevron/go-log.v0/log) | ||
|
||
**go-log** is a simple logging library for [Go](https://golang.org/). | ||
|
||
For package documentation, refer to the GoDoc badge above. | ||
|
||
## Installation | ||
|
||
``` | ||
go get gopkg.in/zhevron/go-log.v0/log | ||
``` | ||
**log** is a simple logging library for [Google Go](https://golang.org/). | ||
For full package documentation, see the GoDoc link above. | ||
|
||
## Usage | ||
|
||
### Log to file | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"gopkg.in/zhevron/go-log.v0/log" | ||
) | ||
|
||
func main() { | ||
// Open the log file. | ||
f, err := os.Create("myLogFile.log") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
// Create and open a file. | ||
f, err := os.Create("myLogFile.log") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
|
||
// Create a new logger. | ||
logger := log.NewLogger("MyLogger", log.Info) | ||
// Add the file as an output. | ||
log.AddOutput(f) | ||
|
||
// Add the file as an output. | ||
logger.Output(f) | ||
// Create a new logger. | ||
logger := log.NewLogger("MyLogger", log.Info) | ||
|
||
logger.Info("This message will appear in the log file") | ||
} | ||
logger.Info("This message will appear in the log file") | ||
``` | ||
|
||
### Log to stdout | ||
|
||
```go | ||
package main | ||
// Add stdout as an output. | ||
log.AddOutput(os.Stdout) | ||
|
||
import "gopkg.in/zhevron/go-log.v0/log" | ||
// Create a new logger. | ||
logger := log.NewLogger("MyLogger", log.Info) | ||
|
||
func main() { | ||
// Create a new logger. | ||
logger := log.NewLogger("MyLogger", log.Info) | ||
|
||
// Add stdout as an output. | ||
logger.Output(os.Stdout) | ||
|
||
logger.Info("This message will appear in stdout") | ||
} | ||
logger.Info("This message will appear in stdout") | ||
``` | ||
|
||
## License | ||
|
||
**go-log** is licensed under the [MIT license](http://opensource.org/licenses/MIT). | ||
**log** is licensed under the [MIT license](http://opensource.org/licenses/MIT). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package log provides a simple logging interface. | ||
package log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package log | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/zhevron/log" | ||
) | ||
|
||
var logger = NewLogger("ExampleLogger", Info) | ||
|
||
func ExampleAddOutput() { | ||
f, err := os.Create("myLogFile.log") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
log.AddOutput(f) | ||
} | ||
|
||
func ExampleLoggerDebug() { | ||
logger.Debug("This is a debug message") | ||
} | ||
|
||
func ExampleLoggerDebugf() { | ||
msg := "This is a debug message" | ||
logger.Debugf("%s", msg) | ||
} | ||
|
||
func ExampleLoggerInfo() { | ||
logger.Info("This is an info message") | ||
} | ||
|
||
func ExampleLoggerInfof() { | ||
msg := "This is an info message" | ||
logger.Infof("%s", msg) | ||
} | ||
|
||
func ExampleLoggerWarning() { | ||
logger.Warning("This is a warning message") | ||
} | ||
|
||
func ExampleLoggerWarningf() { | ||
msg := "This is a warning message" | ||
logger.Warningf("%s", msg) | ||
} | ||
|
||
func ExampleLoggerError() { | ||
logger.Error("This is an error message") | ||
} | ||
|
||
func ExampleLoggerErrorf() { | ||
msg := "This is an error message" | ||
logger.Errorf("%s", msg) | ||
} | ||
|
||
func ExampleLoggerFatal() { | ||
logger.Fatal("This is a fatal message") | ||
} | ||
|
||
func ExampleLoggerFatalf() { | ||
msg := "This is a fatal message" | ||
logger.Fatalf("%s", msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package log | ||
|
||
type level uint8 | ||
|
||
const ( | ||
// Debug is used for messages related to debugging. | ||
Debug level = iota | ||
// Info is used for informational messages. | ||
Info | ||
// Warning is used for warnings. | ||
Warning | ||
// Error is used for non-fatal errors. | ||
Error | ||
// Fatal used for fatal errors. | ||
Fatal | ||
) | ||
|
||
func (l level) String() string { | ||
switch l { | ||
case Debug: | ||
return "DEBUG" | ||
|
||
case Info: | ||
return "INFO" | ||
|
||
case Warning: | ||
return "WARNING" | ||
|
||
case Error: | ||
return "ERROR" | ||
|
||
case Fatal: | ||
return "FATAL" | ||
|
||
default: | ||
return "UNKNOWN" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package log | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/zhevron/match" | ||
) | ||
|
||
var levelTests = []struct { | ||
in level | ||
out string | ||
}{ | ||
{Debug, "DEBUG"}, | ||
{Info, "INFO"}, | ||
{Warning, "WARNING"}, | ||
{Error, "ERROR"}, | ||
{Fatal, "FATAL"}, | ||
{level(255), "UNKNOWN"}, | ||
} | ||
|
||
func TestLevelString(t *testing.T) { | ||
for _, tt := range levelTests { | ||
match.Equals(t, tt.in.String(), tt.out) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package log | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
) | ||
|
||
// DefaultTimestampFormat defines the timestamp format all new loggers will be | ||
// created with. You can override this per logger instance by setting | ||
// Logger.TimestampFormat. | ||
var DefaultTimestampFormat = "2006-01-02T15:04:05-07:00" | ||
|
||
// DefaultShowFileAndLineNumber defines whether the ShowFileAndLineNumber | ||
// property on new Logger instances is enabled by default. | ||
var DefaultShowFileAndLineNumber = false | ||
|
||
// DefaultUseUTC defines whether the UseUTC property on new Logger instances is | ||
// enabled by default. | ||
var DefaultUseUTC = false | ||
|
||
type writer struct { | ||
writer io.Writer | ||
mutex sync.Mutex | ||
} | ||
|
||
var loggers []*Logger | ||
|
||
var writers []*writer | ||
|
||
// AddOutput adds one or more io.Writer instances as output sinks for the | ||
// library. If a writer that has previously been added is passed, it will be | ||
// ignored. | ||
func AddOutput(ioWriters ...io.Writer) { | ||
for i := range ioWriters { | ||
for j := range writers { | ||
if ioWriters[i] == writers[j].writer { | ||
ioWriters = append(ioWriters[:i], ioWriters[i+1:]...) | ||
} | ||
} | ||
} | ||
|
||
var newWriters []*writer | ||
for i := range ioWriters { | ||
newWriters = append(newWriters, &writer{writer: ioWriters[i]}) | ||
} | ||
|
||
writers = append(writers, newWriters...) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.