Documentation
¶
Overview ¶
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. The original code is from: 1.blog https://avito.tech/old/tpost/yfhycud6h1-logirovanie-kak-v-avito-goslog 2.code https://github.com/avito-tech/avitotech-presentations/commit/cf5ff7ea041dcdd3846634239d9ac27d5c80a86a
Index ¶
- func CombineContexts(logctx context.Context, requestCtx context.Context) context.Context
- func ErrorCtx(ctx context.Context, err error) context.Context
- func WithLogAppName(ctx context.Context, appName string) context.Context
- func WithLogLanguage(ctx context.Context, language string) context.Context
- func WithLogMessage(ctx context.Context, message string) context.Context
- func WithLogPhone(ctx context.Context, phone string) context.Context
- func WithLogScope(ctx context.Context, envScope string) context.Context
- func WithLogTraceID(ctx context.Context, traceID string) context.Context
- func WithLogType(ctx context.Context, typelog string) context.Context
- func WithLogUserID(ctx context.Context, userID uuid.UUID) context.Context
- func WithSpanID(ctx context.Context, spanID string) context.Context
- func WrapError(ctx context.Context, err error) error
- type HandlerMiddlware
- type MultiHandler
- func (mh *MultiHandler) AddHandler(h slog.Handler)
- func (mh *MultiHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (mh *MultiHandler) Handle(ctx context.Context, r slog.Record) error
- func (mh *MultiHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (mh *MultiHandler) WithGroup(name string) slog.Handler
- type Option
- func WithHandler(handler slog.Handler) Option
- func WithJSONHandler(w io.Writer, opts *slog.HandlerOptions) Option
- func WithOtelSlogOption(serviceName string, opts ...otelslog.Option) Option
- func WithRawHandler(handler slog.Handler) Option
- func WithTextHandler(w io.Writer, opts *slog.HandlerOptions) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CombineContexts ¶
func WithLogAppName ¶
WithLogAppName by execution add appName to log record
func WithLogLanguage ¶
WithLogLanguage by execution add language to log record
func WithLogMessage ¶
WithLogMessage by execution add message to log record
func WithLogPhone ¶
WithLogPhone by execution add phone to log record
func WithLogScope ¶
WithLogScope by execution add scope to log record
func WithLogType ¶
WithLogType by execution add type to log record
func WithLogUserID ¶
WithLogUserID by execution add userID to log record
Types ¶
type HandlerMiddlware ¶
type HandlerMiddlware struct {
// contains filtered or unexported fields
}
func NewHandlerMiddleware ¶
func NewHandlerMiddleware(next slog.Handler) *HandlerMiddlware
NewHandlerMiddleware create new middleware
func (*HandlerMiddlware) Enabled ¶
Enabled reports whether the handler handles records at the given level.
type MultiHandler ¶
type MultiHandler struct {
// contains filtered or unexported fields
}
MultiHandler объединяет несколько slog.Handler и вызывает их при логировании.
func InitLogging ¶
func InitLogging(opts ...Option) *MultiHandler
InitLogging initializes logging with the given options and sets slog.Default. If no handler options (WithJSONHandler, WithTextHandler, WithHandler) are provided, NO default handler is added — you must explicitly choose your format.
Returns *MultiHandler so you can inject more handlers later (e.g. OTel bridge).
func InitLoggingJSON ¶ added in v0.3.0
func InitLoggingJSON(w io.Writer, opts ...Option) *MultiHandler
InitLoggingJSON is a convenience shortcut that behaves like the old InitLogging: creates a JSON handler writing to w, wraps it in HandlerMiddleware, applies extra opts.
func NewMultiHandler ¶
func NewMultiHandler(handlers ...slog.Handler) *MultiHandler
NewMultiHandler создаёт новый мульти-обработчик.
func (*MultiHandler) AddHandler ¶ added in v0.3.0
func (mh *MultiHandler) AddHandler(h slog.Handler)
AddHandler добавляет хендлер в рантайме (thread-safe). Позволяет inject'ить хендлеры после создания логгера.
func (*MultiHandler) Enabled ¶
Enabled возвращает true, если хотя бы один из обработчиков включён для данного уровня.
type Option ¶
type Option func(*MultiHandler)
func WithHandler ¶ added in v0.3.0
WithHandler adds any custom slog.Handler. The handler is wrapped with HandlerMiddleware for context extraction.
func WithJSONHandler ¶ added in v0.3.0
func WithJSONHandler(w io.Writer, opts *slog.HandlerOptions) Option
WithJSONHandler adds slog.JSONHandler as the primary handler.
func WithOtelSlogOption ¶
WithOtelSlogOption adds OpenTelemetry slog bridge handler.
func WithRawHandler ¶ added in v0.3.0
WithRawHandler adds any custom slog.Handler WITHOUT wrapping in HandlerMiddleware. Use this when you handle context extraction yourself.
func WithTextHandler ¶ added in v0.3.0
func WithTextHandler(w io.Writer, opts *slog.HandlerOptions) Option
WithTextHandler adds slog.TextHandler as the primary handler.