Skip to content

Commit 66a8e50

Browse files
committed
Update to 8.1.0 - a new logger implemented as a solution for #680
1 parent b46f3e7 commit 66a8e50

File tree

14 files changed

+78
-50
lines changed

14 files changed

+78
-50
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ go:
77
- tip
88
go_import_path: github.com/kataras/iris
99
install:
10-
- go get ./... # for iris-contrib/httpexpect and sirupsen/logrus
10+
- go get ./... # for iris-contrib/httpexpect and kataras/golog
1111
script:
1212
- go test -v -cover ./...
1313
after_script:

HISTORY.md

+36
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,46 @@
1313
### Should I upgrade my Iris?
1414

1515
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
16+
1617
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
1718
1819
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
1920

21+
22+
# We, 26 July 2017 | v8.1.0
23+
24+
The `app.Logger() *logrus.Logger` was replaced with a custom implementation [[golog](https://github.com/kataras/golog)], it's compatible with the [logrus](https://github.com/sirupsen/logrus) package and other open-source golang loggers as well, because of that: https://github.com/kataras/iris/issues/680#issuecomment-316184570.
25+
26+
The API didn't change much except these:
27+
28+
- the new implementation does not recognise `Fatal` and `Panic` because, actually, iris never panics
29+
- the old `app.Logger().Out = io.Writer` should be written as `app.Logger().SetOutput(io.Writer)`
30+
31+
The new implementation, [golog](https://github.com/kataras/golog) is more featured
32+
and it completes more use cases than the before external implementation.
33+
34+
At general you have to know that the low-level relative fields and functions are actually inside `app.Logger().Printer` object, i.e: `app.Logger().Printer.Output` to get the `io.Writer` or `app.Logger().Printer.AddOuput/SetOutput` to set or add more output(`io.Writer`) targets.
35+
36+
### Integration
37+
38+
I understand that many of you may use logrus outside of Iris too. To integrate an external `logrus` logger just
39+
`Install` it-- all print operations will be handled by the provided `logrus instance`.
40+
41+
```go
42+
import (
43+
"github.com/kataras/iris"
44+
"github.com/sirupsen/logrus"
45+
)
46+
47+
package main(){
48+
app := iris.New()
49+
app.Logger().Install(logrus.StandardLogger()) // the package-level logrus instance
50+
// [...]
51+
}
52+
```
53+
54+
For more information about our new logger please navigate to: https://github.com/kataras/golog - contributions are welcomed as well!
55+
2056
# Sa, 23 July 2017 | v8.0.7
2157

2258
Fix [It's true that with UseGlobal the "/path1.txt" route call the middleware but cause the prepend, the order is inversed](https://github.com/kataras/iris/issues/683#issuecomment-317229068)

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Iris is a fast, simple and efficient micro web framework for Go. It provides a b
1717
### 📑 Table of contents
1818

1919
* [Installation](#-installation)
20-
* [Latest changes](https://github.com/kataras/iris/blob/master/HISTORY.md#sa-23-july-2017--v807)
20+
* [Latest changes](https://github.com/kataras/iris/blob/master/HISTORY.md#we-26-july-2017--v810)
2121
* [Learn](#-learn)
2222
* [HTTP Listening](_examples/#http-listening)
2323
* [Configuration](_examples/#configuration)
@@ -339,7 +339,7 @@ Thank You for your trust!
339339

340340
### 📌 Version
341341

342-
Current: **8.0.7**
342+
Current: **8.1.0**
343343

344344
Each new release is pushed to the master. It stays there until the next version. When a next version is released then the previous version goes to its own branch with `gopkg.in` as its import path (and its own vendor folder), in order to keep it working "for-ever".
345345

_examples/http-listening/listen-addr/omit-server-errors/main_test.go

+5-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"bytes"
55
stdContext "context"
6-
"fmt"
76
"strings"
87
"testing"
98
"time"
@@ -14,7 +13,7 @@ import (
1413
func logger(app *iris.Application) *bytes.Buffer {
1514
buf := &bytes.Buffer{}
1615

17-
app.Logger().Out = buf
16+
app.Logger().SetOutput(buf)
1817

1918
// disable the "Now running at...." in order to have a clean log of the error.
2019
// we could attach that on `Run` but better to keep things simple here.
@@ -41,20 +40,12 @@ func TestListenAddr(t *testing.T) {
4140
t.Fatalf("expecting err to be `iris.ErrServerClosed` but got: %v", err)
4241
}
4342

44-
// println(log.Bytes())
45-
// println(len(log.Bytes()))
43+
expectedMessage := iris.ErrServerClosed.Error()
4644

47-
expected := fmt.Sprintln("\"" + iris.ErrServerClosed.Error() + "\" ")
48-
expected = strings.TrimSpace(expected)
49-
// println([]byte(expected))
50-
// println(len([]byte(expected)))
51-
52-
got := log.String()
53-
got = strings.Split(got, "msg=")[1]
54-
got = strings.TrimSpace(got)
55-
if expected != got {
56-
t.Fatalf("expecting to log the:\n'%s'\ninstead of:\n'%s'", expected, got)
45+
if got := log.String(); !strings.Contains(got, expectedMessage) {
46+
t.Fatalf("expecting to log to contains the:\n'%s'\ninstead of:\n'%s'", expectedMessage, got)
5747
}
48+
5849
}
5950

6051
func TestListenAddrWithoutServerErr(t *testing.T) {

_examples/http_request/request-logger/main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ func main() {
1818
Method: true,
1919
// Path displays the request path
2020
Path: true,
21-
// Columns: true,
21+
22+
//Columns: true,
2223

2324
// if !empty then its contents derives from `ctx.Values().Get("logger_message")
2425
// will be added to the logs.
@@ -57,6 +58,6 @@ func main() {
5758
// http://localhost:8080/2
5859
// http://lcoalhost:8080/notfoundhere
5960
// see the output on the console.
60-
app.Run(iris.Addr(":8080"))
61+
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
6162

6263
}

_examples/http_request/request-logger/request-logger-file/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func main() {
3636
// http://localhost:8080/1
3737
// http://localhost:8080/2
3838
// http://lcoalhost:8080/notfoundhere
39-
app.Run(iris.Addr(":8080"))
39+
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
4040
}
4141

4242
// get a filename based on the date, file logs works that way the most times

_examples/miscellaneous/file-logger/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ func main() {
3232

3333
app := iris.New()
3434
// attach the file as logger, remember, iris' app logger is just an io.Writer.
35-
app.Logger().Out = newLogFile()
35+
app.Logger().SetOutput(newLogFile())
3636

3737
app.Get("/", func(ctx context.Context) {
3838
// for the sake of simplicity, in order see the logs at the ./_today_.txt
39-
ctx.Application().Logger().Infoln("Request path: " + ctx.Path())
39+
ctx.Application().Logger().Info("Request path: " + ctx.Path())
4040
ctx.Writef("hello")
4141
})
4242

4343
// navigate to http://localhost:8080
4444
// and open the ./logs.txt file
4545
if err := app.Run(iris.Addr(":8080"), iris.WithoutBanner); err != nil {
4646
if err != iris.ErrServerClosed {
47-
app.Logger().Warnln("Shutdown with error: " + err.Error())
47+
app.Logger().Warn("Shutdown with error: " + err.Error())
4848
}
4949
}
5050
}

_examples/websocket/secure/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func main() {
4040
// using the go v1.8's HTTP/2 Push.
4141
// Note that you have to listen using ListenTLS in order this to work.
4242
if err := ctx.ResponseWriter().Push("/js/chat.js", nil); err != nil {
43-
ctx.Application().Logger().Warnln(err.Error())
43+
ctx.Application().Logger().Warn(err.Error())
4444
}
4545
ctx.ViewData("", clientPage{"Client Page", ctx.Host()})
4646
ctx.View("client.html")

context/application.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"io"
55
"net/http"
66

7-
"github.com/sirupsen/logrus"
7+
"github.com/kataras/golog"
88
)
99

1010
// Application is the context's owner.
@@ -14,8 +14,8 @@ type Application interface {
1414
// ConfigurationReadOnly returns all the available configuration values can be used on a request.
1515
ConfigurationReadOnly() ConfigurationReadOnly
1616

17-
// Logger returns the logrus logger instance(pointer) that is being used inside the "app".
18-
Logger() *logrus.Logger
17+
// Logger returns the golog logger instance(pointer) that is being used inside the "app".
18+
Logger() *golog.Logger
1919

2020
// View executes and write the result of a template file to the writer.
2121
//

context/context.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2231,7 +2231,7 @@ func (ctx *context) BeginTransaction(pipe func(t *Transaction)) {
22312231
t := newTransaction(ctx) // it calls this *context, so the overriding with a new pool's New of context.Context wil not work here.
22322232
defer func() {
22332233
if err := recover(); err != nil {
2234-
ctx.Application().Logger().Warnln(errTransactionInterrupted.Format(err).Error())
2234+
ctx.Application().Logger().Warn(errTransactionInterrupted.Format(err).Error())
22352235
// complete (again or not , doesn't matters) the scope without loud
22362236
t.Complete(nil)
22372237
// we continue as normal, no need to return here*

doc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Source code and other details for the project are available at GitHub:
3535
3636
Current Version
3737
38-
8.0.7
38+
8.1.0
3939
4040
Installation
4141

iris.go

+16-17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"sync"
1111
"time"
1212

13-
"github.com/sirupsen/logrus"
13+
"github.com/kataras/golog"
1414

1515
// context for the handlers
1616
"github.com/kataras/iris/context"
@@ -33,7 +33,7 @@ import (
3333
const (
3434

3535
// Version is the current version number of the Iris Web Framework.
36-
Version = "8.0.7"
36+
Version = "8.1.0"
3737
)
3838

3939
// HTTP status codes as registered with IANA.
@@ -137,8 +137,8 @@ type Application struct {
137137
// all fields defaults to something that is working, developers don't have to set it.
138138
config *Configuration
139139

140-
// the logrus logger instance, defaults to "Info" level messages (all except "Debug")
141-
logger *logrus.Logger
140+
// the golog logger instance, defaults to "Info" level messages (all except "Debug")
141+
logger *golog.Logger
142142

143143
// view engine
144144
view view.View
@@ -163,7 +163,7 @@ func New() *Application {
163163

164164
app := &Application{
165165
config: &config,
166-
logger: logrus.New(),
166+
logger: golog.Default,
167167
APIBuilder: router.NewAPIBuilder(),
168168
Router: router.NewRouter(),
169169
}
@@ -207,20 +207,19 @@ func (app *Application) ConfigurationReadOnly() context.ConfigurationReadOnly {
207207
// These are the different logging levels. You can set the logging level to log
208208
// on the application 's instance of logger, obtained with `app.Logger()`.
209209
//
210-
// These are conversions from logrus.
210+
// These are conversions from golog.
211211
const (
212212
// NoLog level, logs nothing.
213-
// It's the logrus' `PanicLevel` but it never used inside iris so it will never log.
214-
NoLog = logrus.PanicLevel
213+
NoLog = golog.DisableLevel
215214
// ErrorLevel level. Logs. Used for errors that should definitely be noted.
216215
// Commonly used for hooks to send errors to an error tracking service.
217-
ErrorLevel = logrus.ErrorLevel
216+
ErrorLevel = golog.ErrorLevel
218217
// WarnLevel level. Non-critical entries that deserve eyes.
219-
WarnLevel = logrus.WarnLevel
218+
WarnLevel = golog.WarnLevel
220219
)
221220

222-
// Logger returns the logrus logger instance(pointer) that is being used inside the "app".
223-
func (app *Application) Logger() *logrus.Logger {
221+
// Logger returns the golog logger instance(pointer) that is being used inside the "app".
222+
func (app *Application) Logger() *golog.Logger {
224223
return app.logger
225224
}
226225

@@ -264,13 +263,13 @@ func (app *Application) RegisterView(viewEngine view.Engine) {
264263
func (app *Application) View(writer io.Writer, filename string, layout string, bindingData interface{}) error {
265264
if app.view.Len() == 0 {
266265
err := errors.New("view engine is missing, use `RegisterView`")
267-
app.Logger().Errorln(err)
266+
app.Logger().Error(err)
268267
return err
269268
}
270269

271270
err := app.view.ExecuteWriter(writer, filename, layout, bindingData)
272271
if err != nil {
273-
app.Logger().Errorln(err)
272+
app.Logger().Error(err)
274273
}
275274
return err
276275
}
@@ -328,7 +327,7 @@ func (app *Application) NewHost(srv *http.Server) *host.Supervisor {
328327

329328
// check if different ErrorLog provided, if not bind it with the framework's logger
330329
if srv.ErrorLog == nil {
331-
srv.ErrorLog = log.New(app.logger.Out, "[HTTP Server] ", 0)
330+
srv.ErrorLog = log.New(app.logger.Printer.Output, "[HTTP Server] ", 0)
332331
}
333332

334333
if srv.Addr == "" {
@@ -352,7 +351,7 @@ func (app *Application) NewHost(srv *http.Server) *host.Supervisor {
352351

353352
if !app.config.DisableStartupLog {
354353
// show the available info to exit from app.
355-
su.RegisterOnServe(host.WriteStartupLogOnServe(app.logger.Out)) // app.logger.Writer -> Info
354+
su.RegisterOnServe(host.WriteStartupLogOnServe(app.logger.Printer.Output)) // app.logger.Writer -> Info
356355
}
357356

358357
if !app.config.DisableInterruptHandler {
@@ -543,7 +542,7 @@ func (app *Application) Run(serve Runner, withOrWithout ...Configurator) error {
543542
// this will block until an error(unless supervisor's DeferFlow called from a Task).
544543
err := serve(app)
545544
if err != nil {
546-
app.Logger().Errorln(err)
545+
app.Logger().Error(err)
547546
}
548547
return err
549548
}

middleware/logger/logger.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,15 @@ func (l *requestLoggerMiddleware) ServeHTTP(ctx context.Context) {
7979
logFunc(endTime, latency, status, ip, method, path, message)
8080
return
8181
}
82-
endTimeFormatted := endTime.Format("2006/01/02 - 15:04:05")
82+
8383
if l.config.Columns {
84+
endTimeFormatted := endTime.Format("2006/01/02 - 15:04:05")
8485
output := Columnize(endTimeFormatted, latency, status, ip, method, path, message)
85-
ctx.Application().Logger().Out.Write([]byte(output))
86+
ctx.Application().Logger().Printer.Output.Write([]byte(output))
8687
return
8788
}
8889
// no new line, the framework's logger is responsible how to render each log.
89-
line := fmt.Sprintf("%s | %v %4v %s %s %s", endTimeFormatted, status, latency, ip, method, path)
90+
line := fmt.Sprintf("%v %4v %s %s %s", status, latency, ip, method, path)
9091
if message != nil {
9192
line += fmt.Sprintf(" %v", message)
9293
}

middleware/recover/recover.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func New() context.Handler {
4646
logMessage += fmt.Sprintf("At Request: %s\n", getRequestLogs(ctx))
4747
logMessage += fmt.Sprintf("Trace: %s\n", err)
4848
logMessage += fmt.Sprintf("\n%s", stacktrace)
49-
ctx.Application().Logger().Warnln(logMessage)
49+
ctx.Application().Logger().Warn(logMessage)
5050

5151
ctx.StatusCode(500)
5252
ctx.StopExecution()

0 commit comments

Comments
 (0)