| // Copyright (C) 2016 The Android Open Source Project |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package log |
| |
| // The context key used to store the Severity of a log message in the log context |
| const SeverityKey = "severity" |
| |
| const ( |
| EmergencyLevel = Severity_Emergency |
| AlertLevel = Severity_Alert |
| CriticalLevel = Severity_Critical |
| ErrorLevel = Severity_Error |
| WarningLevel = Severity_Warning |
| NoticeLevel = Severity_Notice |
| InfoLevel = Severity_Info |
| DebugLevel = Severity_Debug |
| ) |
| |
| // GetSeverity gets the severity stored in the given context. |
| func GetSeverity(ctx Context) Severity { |
| s, ok := ctx.Value(SeverityKey).(Severity) |
| if !ok { |
| s = Severity(-1) |
| } |
| return s |
| } |
| |
| // Severity returns a context with the given Severity set on it. |
| func (ctx logContext) Severity(severity Severity) Context { |
| return ctx.setValue(SeverityKey, severity) |
| } |
| |
| // Emergency is shorthand for ctx.At(EmergencyLevel) |
| func (ctx logContext) Emergency() Logger { |
| return ctx.At(EmergencyLevel) |
| } |
| |
| // Alert is is shorthand for ctx.At(AlertLevel) |
| func (ctx logContext) Alert() Logger { |
| return ctx.At(AlertLevel) |
| } |
| |
| // Critical is shorthand for ctx.At(CriticalLevel) |
| func (ctx logContext) Critical() Logger { |
| return ctx.At(CriticalLevel) |
| } |
| |
| // Error is shorthand for ctx.At(ErrorLevel).Cause(err) |
| func (ctx logContext) Error() Logger { |
| return ctx.At(ErrorLevel) |
| } |
| |
| // Warning is shorthand for ctx.At(WarningLevel) |
| func (ctx logContext) Warning() Logger { |
| return ctx.At(WarningLevel) |
| } |
| |
| // Notice is shorthand for ctx.At(NoticeLevel) |
| func (ctx logContext) Notice() Logger { |
| return ctx.At(NoticeLevel) |
| } |
| |
| // Info is shorthand for ctx.At(InfoLevel) |
| func (ctx logContext) Info() Logger { |
| return ctx.At(InfoLevel) |
| } |
| |
| // Debug is shorthand for ctx.At(DebugLevel) |
| func (ctx logContext) Debug() Logger { |
| return ctx.At(DebugLevel) |
| } |
| |
| // DebugN is shorthand for ctx.At(DebugLevel + n) |
| func (ctx logContext) DebugN(n int) Logger { |
| return ctx.At(Severity(int(DebugLevel) + n)) |
| } |