|
| 1 | +package annotations |
| 2 | + |
| 3 | +import ( |
| 4 | + |
| 5 | + "github.com/ARM-software/golang-utils/utils/commonerrors" |
| 6 | + baselogs "github.com/ARM-software/golang-utils/utils/logs" |
| 7 | + "github.com/ARM-software/golang-utils/utils/reflection" |
| 8 | +) |
| 9 | + |
| 10 | +// AnnotationLogger formats annotations and emits them through an underlying |
| 11 | +// logger. |
| 12 | +// |
| 13 | +// The logger delegates final emission to an existing [logs.Loggers] |
| 14 | +// implementation and is therefore suitable for any sink already supported by |
| 15 | +// the logs package. |
| 16 | +type AnnotationLogger struct { |
| 17 | + baselogs.Loggers |
| 18 | + formatter IFormatter |
| 19 | +} |
| 20 | + |
| 21 | +// NewLogger creates an annotation logger backed by baseLogger. |
| 22 | +func NewLogger(baseLogger baselogs.Loggers, formatter IFormatter) (*AnnotationLogger, error) { |
| 23 | + logger := &AnnotationLogger{Loggers: baseLogger, formatter: formatter} |
| 24 | + if err := logger.Check(); err != nil { |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + return logger, nil |
| 28 | +} |
| 29 | + |
| 30 | +func (l *AnnotationLogger) Check() error { |
| 31 | + if reflection.IsEmpty(l.Loggers) { |
| 32 | + return commonerrors.ErrNoLogger |
| 33 | + } |
| 34 | + if l.formatter == nil { |
| 35 | + return commonerrors.UndefinedVariable("annotation formatter") |
| 36 | + } |
| 37 | + return l.Loggers.Check() |
| 38 | +} |
| 39 | + |
| 40 | +// WriteAnnotation writes annotation using the configured formatter. |
| 41 | +func (l *AnnotationLogger) WriteAnnotation(annotation *Annotation) error { |
| 42 | + if err := l.Check(); err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + if reflection.IsEmpty(annotation) { |
| 46 | + return commonerrors.UndefinedVariable("annotation") |
| 47 | + } |
| 48 | + line := l.formatter.Format(annotation) |
| 49 | + switch annotation.Severity { |
| 50 | + case SeverityError: |
| 51 | + l.Loggers.LogError(line) |
| 52 | + default: |
| 53 | + l.Loggers.Log(line) |
| 54 | + } |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +// WriteError writes an error-level annotation. |
| 59 | +func (l *AnnotationLogger) WriteError(message string, options ...AnnotationOption) error { |
| 60 | + annotation := newAnnotation(SeverityError, message, options...) |
| 61 | + return l.WriteAnnotation(&annotation) |
| 62 | +} |
| 63 | + |
| 64 | +// WriteWarning writes a warning-level annotation. |
| 65 | +func (l *AnnotationLogger) WriteWarning(message string, options ...AnnotationOption) error { |
| 66 | + annotation := newAnnotation(SeverityWarning, message, options...) |
| 67 | + return l.WriteAnnotation(&annotation) |
| 68 | +} |
| 69 | + |
| 70 | +// WriteNotice writes a notice-level annotation. |
| 71 | +func (l *AnnotationLogger) WriteNotice(message string, options ...AnnotationOption) error { |
| 72 | + annotation := newAnnotation(SeverityNotice, message, options...) |
| 73 | + return l.WriteAnnotation(&annotation) |
| 74 | +} |
| 75 | + |
| 76 | +var _ IAnnotationLogger = (*AnnotationLogger)(nil) |
0 commit comments