MCPcopy
hub / github.com/caddyserver/caddy / openLogs

Method openLogs

logging.go:86–135  ·  logging.go::Logging.openLogs

openLogs sets up the config and opens all the configured writers. It closes its logs when ctx is canceled, so it should clean up after itself.

(ctx Context)

Source from the content-addressed store, hash-verified

84// It closes its logs when ctx is canceled, so it should clean up
85// after itself.
86func (logging *Logging) openLogs(ctx Context) error {
87 // make sure to deallocate resources when context is done
88 ctx.OnCancel(func() {
89 err := logging.closeLogs()
90 if err != nil {
91 Log().Error("closing logs", zap.Error(err))
92 }
93 })
94
95 // set up the "sink" log first (std lib's default global logger)
96 if logging.Sink != nil {
97 err := logging.Sink.provision(ctx, logging)
98 if err != nil {
99 return fmt.Errorf("setting up sink log: %v", err)
100 }
101 }
102
103 // as a special case, set up the default structured Caddy log next
104 if err := logging.setupNewDefault(ctx); err != nil {
105 return err
106 }
107
108 // then set up any other custom logs
109 for name, l := range logging.Logs {
110 // the default log is already set up
111 if name == DefaultLoggerName {
112 continue
113 }
114
115 err := l.provision(ctx, logging)
116 if err != nil {
117 return fmt.Errorf("setting up custom log '%s': %v", name, err)
118 }
119
120 // Any other logs that use the discard writer can be deleted
121 // entirely. This avoids encoding and processing of each
122 // log entry that would just be thrown away anyway. Notably,
123 // we do not reach this point for the default log, which MUST
124 // exist, otherwise core log emissions would panic because
125 // they use the Log() function directly which expects a non-nil
126 // logger. Even if we keep logs with a discard writer, they
127 // have a nop core, and keeping them at all seems unnecessary.
128 if _, ok := l.writerOpener.(*DiscardWriter); ok {
129 delete(logging.Logs, name)
130 continue
131 }
132 }
133
134 return nil
135}
136
137func (logging *Logging) setupNewDefault(ctx Context) error {
138 if logging.Logs == nil {

Callers 1

provisionContextFunction · 0.80

Calls 6

closeLogsMethod · 0.95
setupNewDefaultMethod · 0.95
LogFunction · 0.85
OnCancelMethod · 0.80
ErrorMethod · 0.45
provisionMethod · 0.45

Tested by

no test coverage detected