@@ -56,6 +56,27 @@ import (
5656// return err, ok
5757// }
5858
59+ // AbbreviateLevel is a ReplaceAttr function that abbreviates log level names.
60+ //
61+ // It modifies the attribute if it's a log level (slog.Level) and changes the level name to its abbreviation.
62+ // The abbreviations are:
63+ //
64+ // - "DEBUG" becomes "DBG"
65+ // - "INFO" becomes "INF"
66+ // - "WARN" becomes "WRN"
67+ // - "ERROR" becomes "ERR"
68+ //
69+ // If the attribute's value is not a slog.Level, it is returned unchanged.
70+ //
71+ // Example:
72+ //
73+ // // Create a logger with the ReplaceAttr function.
74+ // logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
75+ // ReplaceAttr: AbbreviateLevel,
76+ // }))
77+ //
78+ // // Log a message.
79+ // logger.Debug("This is a debug message.") // Output will be: level=DBG msg="This is a debug message."
5980func AbbreviateLevel (_ []string , attr slog.Attr ) slog.Attr {
6081 if attr .Value .Kind () != slog .KindAny {
6182 return attr
@@ -79,6 +100,46 @@ func AbbreviateLevel(_ []string, attr slog.Attr) slog.Attr {
79100 return attr
80101}
81102
103+ // FormatTimes returns a ReplaceAttr function that formats time values according to the specified format.
104+ //
105+ // It modifies an attribute if its value is a time.Time. The time is formatted using the provided format string,
106+ // and the attribute's value is updated to the formatted time string.
107+ //
108+ // If the attribute's value is not a time.Time, it is returned unchanged.
109+ //
110+ // Args:
111+ //
112+ // format string: The format string to use for formatting time values (e.g., time.DateTime, "2006-01-02", "15:04:05").
113+ //
114+ // Returns:
115+ //
116+ // func([]string, slog.Attr) slog.Attr: A ReplaceAttr function that formats time values.
117+ //
118+ // Example:
119+ //
120+ // // Create a ReplaceAttr function that formats times using a custom format.
121+ // customTimeFormat := FormatTimes("2006-01-02 15:04:05")
122+ //
123+ // // Create a logger with the ReplaceAttr function.
124+ // logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
125+ // ReplaceAttr: customTimeFormat,
126+ // }))
127+ //
128+ // // Log a message with a time attribute.
129+ // logger.Info("Time example", slog.Time("now", time.Now()))
130+ // // Output might be: level=INFO msg="Time example" now="2023-10-27 10:30:00"
131+ //
132+ // // Create a ReplaceAttr function that formats times using a predefined format.
133+ // dateTimeFormat := FormatTimes(time.DateTime)
134+ //
135+ // // Create a logger with the ReplaceAttr function.
136+ // logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
137+ // ReplaceAttr: dateTimeFormat,
138+ // }))
139+ //
140+ // // Log a message with a time attribute.
141+ // logger.Info("Time example", slog.Time("now", time.Now()))
142+ // // Output might be: level=INFO msg="Time example" now="2023-10-27 10:30:00.000"
82143func FormatTimes (format string ) func ([]string , slog.Attr ) slog.Attr {
83144 return func (_ []string , a slog.Attr ) slog.Attr {
84145 if a .Value .Kind () == slog .KindTime {
@@ -88,6 +149,20 @@ func FormatTimes(format string) func([]string, slog.Attr) slog.Attr {
88149 }
89150}
90151
152+ // SimpleTime returns a ReplaceAttr function that formats time values to a simple time format: "15:04:05.000".
153+ //
154+ // It's a convenience function that uses FormatTimes internally with a predefined format.
155+ //
156+ // Example:
157+ //
158+ // // Create a logger with the ReplaceAttr function.
159+ // logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
160+ // ReplaceAttr: SimpleTime(),
161+ // }))
162+ //
163+ // // Log a message with a time attribute.
164+ // logger.Info("Time example", slog.Time("now", time.Now()))
165+ // // Output might be: level=INFO msg="Time example" now="10:30:00.000"
91166func SimpleTime () func ([]string , slog.Attr ) slog.Attr {
92167 return FormatTimes ("15:04:05.000" )
93168}
0 commit comments