Documentation
¶
Overview ¶
Package gaelog provides easy Stackdriver Logging on Google App Engine Standard second generation runtimes.
Index ¶
- Constants
- func Alert(ctx context.Context, v interface{})
- func Alertf(ctx context.Context, format string, v ...interface{})
- func Critical(ctx context.Context, v interface{})
- func Criticalf(ctx context.Context, format string, v ...interface{})
- func Debug(ctx context.Context, v interface{})
- func Debugf(ctx context.Context, format string, v ...interface{})
- func Emergency(ctx context.Context, v interface{})
- func Emergencyf(ctx context.Context, format string, v ...interface{})
- func Error(ctx context.Context, v interface{})
- func Errorf(ctx context.Context, format string, v ...interface{})
- func Info(ctx context.Context, v interface{})
- func Infof(ctx context.Context, format string, v ...interface{})
- func Log(ctx context.Context, severity logging.Severity, v interface{})
- func Logf(ctx context.Context, severity logging.Severity, format string, ...)
- func Notice(ctx context.Context, v interface{})
- func Noticef(ctx context.Context, format string, v ...interface{})
- func Warning(ctx context.Context, v interface{})
- func Warningf(ctx context.Context, format string, v ...interface{})
- func Wrap(h http.Handler, options ...logging.LoggerOption) http.Handler
- func WrapWithID(h http.Handler, logID string, options ...logging.LoggerOption) http.Handler
- type Logger
- func (lg *Logger) Alert(v interface{})
- func (lg *Logger) Alertf(format string, v ...interface{})
- func (lg *Logger) Close() error
- func (lg *Logger) Critical(v interface{})
- func (lg *Logger) Criticalf(format string, v ...interface{})
- func (lg *Logger) Debug(v interface{})
- func (lg *Logger) Debugf(format string, v ...interface{})
- func (lg *Logger) Emergency(v interface{})
- func (lg *Logger) Emergencyf(format string, v ...interface{})
- func (lg *Logger) Error(v interface{})
- func (lg *Logger) Errorf(format string, v ...interface{})
- func (lg *Logger) Info(v interface{})
- func (lg *Logger) Infof(format string, v ...interface{})
- func (lg *Logger) Log(severity logging.Severity, v interface{})
- func (lg *Logger) Logf(severity logging.Severity, format string, v ...interface{})
- func (lg *Logger) Notice(v interface{})
- func (lg *Logger) Noticef(format string, v ...interface{})
- func (lg *Logger) Warning(v interface{})
- func (lg *Logger) Warningf(format string, v ...interface{})
Examples ¶
Constants ¶
const ( // DefaultLogID is the default log ID of the underlying Stackdriver Logging logger. Request // logs are logged under the ID "request_log", so use "app_log" for consistency. To use a // different ID create your logger with NewWithID. DefaultLogID = "app_log" // GAEAppResourceType is the type set on the logger's MonitoredResource for App Engine apps. // This matches the type that App Engine itself assigns to request logs. GAEAppResourceType = "gae_app" // CloudRunResourceType is the type set on the logger's MonitoredResource for Cloud Run revisions. // This matches the type that Cloud Run itself assigns to request logs. CloudRunResourceType = "cloud_run_revision" )
Variables ¶
This section is empty.
Functions ¶
func Emergencyf ¶ added in v0.2.0
Emergencyf calls Logf with emergency severity.
func Log ¶ added in v0.2.0
Log logs with the given severity. v must be either a string, or something that marshals via the encoding/json package to a JSON object (and not any other type of JSON value). This should be called from a handler that has been wrapped with Wrap or WrapWithID. If it is called from a handler that has not been wrapped then messages are simply logged using the standard library's log package.
func Logf ¶ added in v0.2.0
Logf logs with the given severity. Remaining arguments are handled in the manner of fmt.Printf. This should be called from a handler that has been wrapped with Wrap or WrapWithID. If it is called from a handler that has not been wrapped then messages are simply logged using the standard library's log package.
func Wrap ¶ added in v0.2.0
Wrap is identical to WrapWithID with the exception that it uses the default log ID.
Example ¶
package main import ( "fmt" "log" "net/http" "os" "github.com/mtraver/gaelog" ) func main() { handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() gaelog.Warningf(ctx, "Some important info right here, that's for sure") fmt.Fprintf(w, "Hey") }) http.Handle("/", gaelog.Wrap(handler)) port := os.Getenv("PORT") if port == "" { port = "8080" } log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil)) }
Output:
func WrapWithID ¶ added in v0.2.0
WrapWithID wraps a handler such that the request's context may be used to call the package-level logging functions. See NewWithID for details on this function's arguments and how the logger is created.
Example ¶
package main import ( "fmt" "log" "net/http" "os" "github.com/mtraver/gaelog" ) func main() { handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() gaelog.Warningf(ctx, "Some important info right here, that's for sure") fmt.Fprintf(w, "Hey") }) http.Handle("/", gaelog.WrapWithID(handler, "my_log")) port := os.Getenv("PORT") if port == "" { port = "8080" } log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil)) }
Output:
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
A Logger logs messages to Stackdriver Logging (though in certain cases it may fall back to the standard library's "log" package; see New). Logs will be correlated with requests in Stackdriver.
func NewWithID ¶
NewWithID creates a new Logger. The Logger is initialized using environment variables that are present on App Engine:
- GOOGLE_CLOUD_PROJECT
- GAE_SERVICE
- GAE_VERSION
If they are not present then it is initialized using environment variables present on Cloud Run:
- K_SERVICE
- K_REVISION
- K_CONFIGURATION
- Project ID is fetched from the metadata server, not an env var
The given log ID will be passed through to the underlying Stackdriver Logging logger.
Additionally, options (of type LoggerOption, from cloud.google.com/go/logging) will be passed through to the underlying Stackdriver Logging logger. Note that the option CommonResource will have no effect because the MonitoredResource is set when each log entry is made, thus overriding any value set with CommonResource. This is intended: much of the value of this package is in setting up the MonitoredResource so that log entries correlate with requests.
The Logger will be valid in all cases, even when the error is non-nil. In the case of a non-nil error the Logger will fall back to the standard library's "log" package. There are three cases in which the error will be non-nil:
- Any of the aforementioned environment variables are not set.
- The given http.Request does not have the X-Cloud-Trace-Context header.
- Initialization of the underlying Stackdriver Logging client produced an error.
func (*Logger) Close ¶
Close closes the Logger, ensuring all logs are flushed and closing the underlying Stackdriver Logging client.
func (*Logger) Critical ¶
func (lg *Logger) Critical(v interface{})
Critical calls Log with critical severity.
func (*Logger) Emergency ¶
func (lg *Logger) Emergency(v interface{})
Emergency calls Log with emergency severity.
func (*Logger) Emergencyf ¶
Emergencyf calls Logf with emergency severity.
func (*Logger) Log ¶
Log logs with the given severity. v must be either a string, or something that marshals via the encoding/json package to a JSON object (and not any other type of JSON value).
func (*Logger) Logf ¶
Logf logs with the given severity. Remaining arguments are handled in the manner of fmt.Printf.
func (*Logger) Notice ¶
func (lg *Logger) Notice(v interface{})
Notice calls Log with notice severity.