I would love to be able to just log using gin, so I don't have to import anything else.

I love it, it is simple, colored, fantastic. I'm quite new to Go and gin, and I'm not sure how to solve.

I was expecting, or better said, thought it would use something like an internal logger.

Is there any way to log As gin? or using gin's internal logger

[INFO] Starting Server
[GIN] 2020/05/29 - 08:37:58 | 200 |     116.336µs |             ::1 | GET      "/api/v1/status/ping"
[GIN] 2020/05/29 - 08:37:58 | 404 |         656ns |             ::1 | GET      "/favicon.ico"

looks like:

Screen Shot 2020-05-29 at 8 45 20 AM

package main

import (
    "github.com/gin-gonic/gin"
)

func main() {
    g := gin.Default()
        gin.DefaultWriter.Write([]byte("[INFO] Starting Server\n"))
    router.Run(":8080")
}

Environment

  • go version: latest
  • gin version (or commit ref): latest
  • operating system: OSX (Darwin)

Comment From: yakuter

Hello @franky-continu Gin is a great tool but if you want to use it just for logging, this is a bad choice. Because it is a full featured API framework, not a simple logger to be used.

You should try https://github.com/sirupsen/logrus for logging.

687474703a2f2f692e696d6775722e636f6d2f505937714d77642e706e67

Comment From: franky-continu

@yakuter thanks for your reply, that logger seems pretty neat but why whould you think I want to use Gin just for logging?

Of course I am creating REST API, I don't want to use a library for each item if I can prevent it, and gin seems to have the idea because it does things mainly on its own.

I was just looking for a way to print using the same internal methods/functions/etc.

Comment From: yakuter

Aaa sorry @franky-continu , I saw "... just log using gin" so I misunderstood I think :)

Only color setting I found in gin is here: https://github.com/gin-gonic/gin#controlling-log-output-coloring

Hope it helps.

Comment From: franky-continu

Yeah I saw the same and even tried to get the writer. I'll probably end up getting a logger compatible with gin.

I use bunyan in nodejs but those logs only look good on my terminal window, we use cloud 66 orchestration and it is difficult to show those logs neatly.

As many other devs I started my logging with JSON fascinated by the endless possibilities but ended up being pretty disappointed with the way bunyan showed up in most outputs. Not their fault at all, just the way we use logs.

Comment From: appleboy

try https://github.com/gin-contrib/logger

Comment From: franky-continu

Thanks @appleboy I appreciate the response, I'm gonna have to end up with a logger I guess.

I kind of don't want to use json TBH but I believe the logger has some different outputs.

Anyhow, thanks so much for the reponse.

Comment From: darnes

I would love to be able to just log using gin, so I don't have to import anything else.

hi @franky-continu found your message googling for exactly same thing. Here is what I managed to find out:

There is no such thing as a default gin logging. Colourful logging is implemented with logging middleware here. It doesn't allow you to use the same format or re-use any of its functions - only thing it does is fancy logging of web-requests. I'm guessing the confusion might originate from the way you init gin ie:

r := gin.Default()

But if you take a look into the implementation:

func Default() *Engine {
    debugPrintWARNINGDefault()
    engine := New()
    engine.Use(Logger(), Recovery())
    return engine
}

it adds the logger middleware so it can output the requests information.

Probably the next best thing to what you want to do is re-use default writer:

fmt.Fprint(gin.DefaultWriter, "yo-ho-ho")

ie:

package main

import (
    "fmt"

    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()
    r.GET("/health_check", func(c *gin.Context) {
        fmt.Fprintln(gin.DefaultWriter, "yo-ho-ho")
        c.JSON(200, gin.H{
            "status": "ok",
        })
    })
    r.Run(":8081")
}