sirupsen / logrus

Structured, pluggable logging for Go.
MIT License
24.8k stars 2.27k forks source link

Customizable Field Name for Log Message #1442

Closed Luigi31415 closed 2 months ago

Luigi31415 commented 2 months ago

Description:

Having the ability to customize the key for the default msg field during logger initialization would provide greater flexibility in shaping log output to meet diverse formatting requirements. This enhancement would not only make it easier to adapt Logrus to existing logging standards but also enable teams to ensure consistency across different systems and workflows.

Proposed Solution:

Introduce a new field in the TextFormatter (and potentially other formatters) to allow users to set a custom field name for msg. For example, this could be done with a CustomFieldKeyMsg option.

log.SetFormatter(&log.TextFormatter{
    DisableColors:     true,
    FullTimestamp:     true,
    CustomFieldKeyMsg: "event",
})

The log output would change from this:

{
    "animal": "walrus",
    "level": "info",
    "msg": "A group of walrus emerges from the ocean",
    "size": 10,
    "time": "2014-03-10 19:57:38.562264131 -0400 EDT"
}

To this:

{
    "animal": "walrus",
    "level": "info",
    "event": "A group of walrus emerges from the ocean",
    "size": 10,
    "time": "2014-03-10 19:57:38.562264131 -0400 EDT"
}
Luigi31415 commented 2 months ago

Trying to implement this, I realized that this functionality has already been implemented. Apologies for the oversight! Thank you for the great work on Logrus.

I’ll go ahead and close this issue.