Skip to content

Commit a4da832

Browse files
committed
minor
1 parent f7561df commit a4da832

File tree

8 files changed

+68
-58
lines changed

8 files changed

+68
-58
lines changed

HISTORY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Introduce the [Formatter](https://github.com/kataras/golog/blob/master/formatter
3030

3131
## Sa 06 June 2020 | v0.0.16
3232

33-
- New `Fields` map type that can be passed to `Logf/Debugf/Infof/Warnf/Errorf/Fatalf` functions and set the `Log.Fields` data field (which can be retrieved through a custom `LogHandler`).
33+
- New `Fields` type that can be passed to `Logf/Debugf/Infof/Warnf/Errorf/Fatalf` functions and set the `Log.Fields` data field (which can be retrieved through a custom `LogHandler`).
3434
- Add `Log.Stacktrace` of new `Frame` type which holds the callers stack trace when `Debug/Debugf`.
3535
- Add `json` struct fields to the `Log` structure.
3636
- Update the [customize-output](_examples/customize-output) example.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2017-2020 Gerasimos Maropoulos.
1+
Copyright (c) 2017-2021 Gerasimos Maropoulos.
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without

_benchmarks/golog_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ func printGolog(i int) {
3030
golog.Errorf("[%d] This is an error message", i)
3131
golog.Warnf("[%d] This is a warning message", i)
3232
golog.Infof("[%d] This is an info message", i)
33-
golog.Debugf("[%d] This is a debug message", i)
33+
// Debug on golog prints the whole stacktrace, while logrus does not, don't include that.
3434
}

_benchmarks/logrus_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,4 @@ func printLogrus(i int) {
2323
logrus.Errorf("[%d] This is an error message", i)
2424
logrus.Warnf("[%d] This is a warning message", i)
2525
logrus.Infof("[%d] This is an info message", i)
26-
logrus.Debugf("[%d] This is a debug message", i)
2726
}

_benchmarks/std_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build ignore
2-
31
package benchmarks
42

53
import (
@@ -27,8 +25,7 @@ func BenchmarkStdPrint(b *testing.B) {
2725
}
2826

2927
func printStd(i int) {
30-
log.Printf("[%d] [%s] This is an error message\n", i, pio.Red("[ERRO]"))
31-
log.Printf("[%d] [%s] This is a warning message\n", i, pio.Purple("[WARN]"))
32-
log.Printf("[%d] [%s] This is an info message\n", i, pio.LightGreen("[INFO]"))
33-
log.Printf("[%d] [%s] This is a debug message\n", i, pio.Yellow("[DBUG]"))
28+
log.Printf("[%d] [%s] This is an error message\n", i, pio.Rich("[ERRO]", pio.Red))
29+
log.Printf("[%d] [%s] This is a warning message\n", i, pio.Rich("[WARN]", pio.Magenta))
30+
log.Printf("[%d] [%s] This is an info message\n", i, pio.Rich("[INFO]", pio.Green))
3431
}

_examples/customize-output/main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func main() {
2626
*/
2727
golog.Debugf("This is a %s with data (debug prints the stacktrace too)", "message", golog.Fields{
2828
"username": "kataras",
29-
})
29+
}) // If more than one golog.Fields passed, then they are merged into a single map.
3030

3131
/* Example Output:
3232
{
@@ -36,12 +36,23 @@ func main() {
3636
"fields": {
3737
"home": "https://iris-go.com"
3838
}
39+
"stacktrace": [...]
3940
}
4041
*/
4142
golog.Infof("An info message", golog.Fields{"home": "https://iris-go.com"})
4243

4344
golog.Warnf("Hey, warning here")
4445
golog.Errorf("Something went wrong!")
46+
47+
// You can also pass custom structs, like normally you would do.
48+
type myCustomData struct {
49+
Username string `json:"username"`
50+
Email string `json:"email"`
51+
}
52+
golog.Fatalf("A fatal error for %s screen!", "home", golog.Fields{"data": myCustomData{
53+
Username: "kataras",
54+
Email: "kataras2006@hotmail.com",
55+
}})
4556
}
4657

4758
/* Manually, use it for any custom format:

doc.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
// Copyright (c) 2017-2020 Gerasimos Maropoulos.
2-
// All rights reserved.
3-
//
4-
// Redistribution and use in source and binary forms, with or without
5-
// modification, are permitted provided that the following conditions are
6-
// met:
7-
//
8-
// * Redistributions of source code must retain the above copyright
9-
// notice, this list of conditions and the following disclaimer.
10-
// * Redistributions in binary form must reproduce the above
11-
// copyright notice, this list of conditions and the following disclaimer
12-
// in the documentation and/or other materials provided with the
13-
// distribution.
14-
// * Neither the name of golog nor the names of its
15-
// contributors may be used to endorse or promote products derived from
16-
// this software without specific prior written permission.
17-
//
18-
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19-
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20-
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21-
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22-
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23-
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24-
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25-
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26-
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27-
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28-
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1+
/*
2+
Copyright (c) 2017-2021 Gerasimos Maropoulos.
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are
7+
met:
8+
9+
* Redistributions of source code must retain the above copyright
10+
notice, this list of conditions and the following disclaimer.
11+
* Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following disclaimer
13+
in the documentation and/or other materials provided with the
14+
distribution.
15+
* Neither the name of golog nor the names of its
16+
contributors may be used to endorse or promote products derived from
17+
this software without specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2930
3031
/*
3132
Package golog provides an easy to use foundation for your logging operations.
@@ -36,7 +37,7 @@ Source code and other details for the project are available at GitHub:
3637
3738
Current Version
3839
39-
0.1.4
40+
0.1.7
4041
4142
Installation
4243
@@ -385,9 +386,8 @@ If you enjoy what you just saw and want to learn more, please follow the below l
385386
Examples:
386387
387388
https://github.com/kataras/golog/tree/master/_examples
388-
389389
*/
390-
package golog // import "github.com/kataras/golog"
390+
package golog
391391

392392
// Version is the version string representation of the "golog" package.
393-
const Version = "0.1.4"
393+
const Version = "0.1.7"

logger.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func New() *Logger {
7373
}
7474
}
7575

76-
// Fields is just a custom type of the map type.
76+
// Fields is a map type.
7777
// One or more values of `Fields` type can be passed
7878
// on all Log methods except `Print/Printf/Println` to set the `Log.Fields` field,
7979
// which can be accessed through a custom LogHandler.
@@ -368,26 +368,26 @@ func (l *Logger) Println(v ...interface{}) {
368368
l.print(DisableLevel, fmt.Sprint(v...), true, nil)
369369
}
370370

371-
func splitArgsFields(v []interface{}) ([]interface{}, Fields) {
371+
func splitArgsFields(values []interface{}) ([]interface{}, Fields) {
372372
var (
373-
args = v[:0]
373+
args = values[:0]
374374
fields Fields
375375
)
376376

377-
for _, val := range v {
378-
f, ok := val.(Fields)
379-
if ok {
380-
if len(fields) > 0 { // upsert new values.
381-
for k, v := range f {
382-
fields[k] = v
383-
}
384-
} else {
385-
fields = f // set fields.
377+
for _, value := range values {
378+
if f, ok := value.(Fields); ok {
379+
if fields == nil {
380+
fields = make(Fields)
381+
}
382+
383+
for k, v := range f {
384+
fields[k] = v
386385
}
386+
387387
continue
388388
}
389389

390-
args = append(args, val) // use it as fmt argument.
390+
args = append(args, value) // use it as fmt argument.
391391
}
392392

393393
return args, fields
@@ -408,8 +408,11 @@ func (l *Logger) Log(level Level, v ...interface{}) {
408408
// It adds a new line in the end.
409409
func (l *Logger) Logf(level Level, format string, args ...interface{}) {
410410
if l.Level >= level {
411-
args, fields := splitArgsFields(args)
412-
msg := fmt.Sprintf(format, args...)
411+
arguments, fields := splitArgsFields(args)
412+
msg := format
413+
if len(arguments) > 0 {
414+
msg = fmt.Sprintf(msg, arguments...)
415+
}
413416
l.print(level, msg, l.NewLine, fields)
414417
}
415418
}

0 commit comments

Comments
 (0)