// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com). // All rights reserved. // contextual-log.go package logimpl import ( "fmt" "git.portale-stac.it/go-pkg/logger" ) type ContextualLog struct { log logger.FullLogger tag string baseStackOffset int } func (clog ContextualLog) Init(log logger.FullLogger, template string, args ...any) { clog.log = log clog.tag = fmt.Sprintf(template, args...) clog.baseStackOffset, _ = log.GetProperty(FULL_LOGGER_STACK_OFFSET).(int) } func NewCtxLog(log logger.FullLogger, template string, args ...any) (clog *ContextualLog) { offset, _ := log.GetProperty(FULL_LOGGER_STACK_OFFSET).(int) return &ContextualLog{ log: log, tag: fmt.Sprintf(template, args...), baseStackOffset: offset, } } func (clog *ContextualLog) Logger() logger.FullLogger { return clog.log } func (clog *ContextualLog) Debugf(templ string, args ...any) { clog.log.SetProperty(FULL_LOGGER_STACK_OFFSET, clog.baseStackOffset+1) clog.log.Debugf(clog.tag+templ, args...) clog.log.SetProperty(FULL_LOGGER_STACK_OFFSET, clog.baseStackOffset) } func (clog *ContextualLog) Infof(templ string, args ...any) { clog.log.SetProperty(FULL_LOGGER_STACK_OFFSET, clog.baseStackOffset+1) clog.log.Infof(clog.tag+templ, args...) clog.log.SetProperty(FULL_LOGGER_STACK_OFFSET, clog.baseStackOffset) } func (clog *ContextualLog) Warnf(templ string, args ...any) { clog.log.SetProperty(FULL_LOGGER_STACK_OFFSET, clog.baseStackOffset+1) clog.log.Warnf(clog.tag+templ, args...) clog.log.SetProperty(FULL_LOGGER_STACK_OFFSET, clog.baseStackOffset) } func (clog *ContextualLog) Errorf(templ string, args ...any) { clog.log.SetProperty(FULL_LOGGER_STACK_OFFSET, clog.baseStackOffset+1) clog.log.Errorf(clog.tag+templ, args...) clog.log.SetProperty(FULL_LOGGER_STACK_OFFSET, clog.baseStackOffset) }