incubator-devlake icon indicating copy to clipboard operation
incubator-devlake copied to clipboard

[Feature][Backend] Option for JSON logging

Open Maniacal opened this issue 2 years ago • 1 comments

Search before asking

  • [X] I had searched in the issues and found no similar feature requirement.

Use case

As an operator, I want to be able to have devlake backend logs output in JSON format

Description

Devlake backend is using github.com/sirupsen/logrus for logging and has hardcoded the logrus.TextFormatter. Logrus also has an option to use a JSONFormatter https://pkg.go.dev/github.com/sirupsen/logrus#JSONFormatter. Would be nice to have the option to get logs in JSON format.

Could be exposed via environment variable LOGGING_FORMAT=json with a default value of text.

Related issues

No response

Are you willing to submit a PR?

  • [ ] Yes I am willing to submit a PR!

Code of Conduct

Maniacal avatar Feb 20 '24 22:02 Maniacal

Collecting votes

klesh avatar Feb 23 '24 06:02 klesh

Heyy @Maniacal, I'm new to Dev-Lake. Looking at the above feature request, I can see that you want to add a JOSNFormatter to view the logs in JSON format. Correct me if I am wrong. We need to add a conditional statement where if the LOGGING_FORMAT is set to JSON, we intend the log to be in JSON format; otherwise, text by default.

We need to modify this file. https://github.com/apache/incubator-devlake/blob/00213f1833374897758b5c4a60be67d872de8d92/backend/impls/logruslog/init.go#L50-L53

Where the code should look something like this:

if format := os.Getenv("LOGGING_FORMAT"); format == "json" {
    inner.SetFormatter(&logrus.JSONFormatter{
    TimestampFormat: time.DateTime
    })
} else {
    inner.SetFormatter(&logrus.TextFormatter{
        TimestampFormat: time.DateTime,
        FullTimestamp:   true,
    })
}

According to me, this is the possible solution. Please share your thoughts on this and tell me whether I should open a PR for it.

hanshal101 avatar Feb 25 '24 09:02 hanshal101

Hi @hanshal101 . I've not developed on this project, but I have worked with logrus in the past and, yes, I believe that is all that would be required to enable this feature.

Maniacal avatar Feb 26 '24 15:02 Maniacal

ohk should I open a PR to fix this

hanshal101 avatar Feb 26 '24 15:02 hanshal101

@Maniacal Would you like to take a look at the PR and see if it solves your problem? Thanks.

klesh avatar Feb 27 '24 03:02 klesh

@Maniacal Would you like to take a look at the PR and see if it solves your problem? Thanks.

From a code perspective it looks like what we're looking for. Reached out to the guys who deploy it internally to see if they're setup to deploy a PR so we can functionally test.

Maniacal avatar Feb 28 '24 14:02 Maniacal

Ok, sounds good @Maniacal

hanshal101 avatar Feb 28 '24 17:02 hanshal101