Nebo15 / logger_json

JSON logger formatter with support for Google Cloud, DataDog and other for Elixir.
https://nebo15.github.io/logger_json/
MIT License
237 stars 92 forks source link

Keep native DateTime, Date and Time struct encoding #125

Closed imricardoramos closed 2 months ago

imricardoramos commented 2 months ago

Right now dates and times are encoded with the struct encoder which makes the logs be very verbose about dates (I'm using Grafana Loki to view logs and it makes it hard to parse visually):

%{level: :info, msg: {:string, "some message"}, meta: %{
  datetime: DateTime.utc_now(), 
  date: Date.utc_today(), 
  time: Time.utc_now
}}
|> LoggerJSON.Formatters.Basic.format(metadata: [:datetime, :date, :time]) 
|> IO.chardata_to_string()
|> Jason.decode! 
|> Jason.encode!(pretty: true) 
|> IO.puts 
{                                                                                                                                                           
  "message": "some message",                                                                                                                 
  "metadata": {                                                                                                                              
    "date": {                                                                                                                                
      "calendar": "Elixir.Calendar.ISO",                                                                                                     
      "day": 29,                                                                                                                             
      "month": 7,                                                                                                                            
      "year": 2024                                                                                                                           
    },                                                                                                                                       
    "datetime": {                                                                                                                            
      "calendar": "Elixir.Calendar.ISO",                                                                                                     
      "day": 29,                                                                                                                             
      "hour": 16,                                                                                                                            
      "microsecond": [                                                                                                                       
        744919,                                                                                                                              
        6                                                                                                                                    
      ],                                                                                                                                     
      "minute": 45,                                                                                                                          
      "month": 7,     
      "second": 0,           
      "std_offset": 0,
      "time_zone": "Etc/UTC",
      "utc_offset": 0,                 
      "year": 2024,                    
      "zone_abbr": "UTC"               
    },                                 
    "time": {                          
      "calendar": "Elixir.Calendar.ISO",                                      
      "hour": 16,                      
      "microsecond": [                 
        744951,                        
        6                              
      ],                               
      "minute": 45,                    
      "second": 0                      
    }                                  
  },                                   
  "severity": "info",                  
  "time": "2024-07-29T16:45:00.744Z"                                          
} 

I propose to keep those as-is and let them be encoded by their native Jason encoding. They don't present any sensitive data and therefore they don't need to be checked for redacted fields.

{                                                                                                                                                           
  "message": "some message",                                                                                                                                
  "metadata": {                                                                                                                                             
    "date": "2024-07-29",                                                                                                                                   
    "datetime": "2024-07-29T16:44:50.502282Z",                                                                                                              
    "time": "16:44:50.502331"                                                                                                                               
  },                                                                                                                                                        
  "severity": "info",                                                                                                                                       
  "time": "2024-07-29T16:44:50.502Z"                                                                                                                        
} 
coveralls commented 2 months ago

Coverage Status

coverage: 100.0%. remained the same when pulling ba2e01474bc8034c6a5dc49911697f525723c6dd on imricardoramos:fix-date-time-encoding into 222e612bda8507b7e9f624e907c15193088fbbcd on Nebo15:master.

AndrewDryga commented 2 months ago

@imricardoramos can you please add a few tests that will cover newly added lines? See: https://coveralls.io/builds/68924173/source?filename=lib%2Flogger_json%2Fformatter%2Fredactor_encoder.ex#L35

They can be added here: https://github.com/Nebo15/logger_json/blob/master/test/logger_json/formatter/redactor_encoder_test.exs

imricardoramos commented 2 months ago

Done