2018-03-14 15:01:47 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2018 Minio, Inc.
|
|
|
|
*
|
|
|
|
* 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 logger
|
|
|
|
|
|
|
|
import "context"
|
|
|
|
|
2018-03-15 16:27:16 -04:00
|
|
|
// Key used for Get/SetContext
|
2018-03-14 21:36:54 -04:00
|
|
|
type contextKeyType string
|
|
|
|
|
|
|
|
const contextLogKey = contextKeyType("miniolog")
|
2018-03-14 15:01:47 -04:00
|
|
|
|
|
|
|
// KeyVal - appended to ReqInfo.Tags
|
|
|
|
type KeyVal struct {
|
|
|
|
Key string
|
|
|
|
Val string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReqInfo stores the request info.
|
|
|
|
type ReqInfo struct {
|
|
|
|
RemoteHost string // Client Host/IP
|
|
|
|
UserAgent string // User Agent
|
|
|
|
RequestID string // x-amz-request-id
|
|
|
|
API string // API name - GetObject PutObject NewMultipartUpload etc.
|
|
|
|
BucketName string // Bucket name
|
|
|
|
ObjectName string // Object name
|
2018-03-15 16:27:16 -04:00
|
|
|
Tags []KeyVal // Any additional info not accommodated by above fields
|
2018-03-14 15:01:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppendTags - appends key/val to ReqInfo.Tags
|
|
|
|
func (r *ReqInfo) AppendTags(key string, val string) {
|
|
|
|
if r == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.Tags = append(r.Tags, KeyVal{key, val})
|
|
|
|
}
|
|
|
|
|
2018-03-15 16:27:16 -04:00
|
|
|
// SetContext sets ReqInfo in the context.
|
|
|
|
func SetContext(ctx context.Context, req *ReqInfo) context.Context {
|
2018-03-14 21:36:54 -04:00
|
|
|
return context.WithValue(ctx, contextLogKey, req)
|
2018-03-14 15:01:47 -04:00
|
|
|
}
|
|
|
|
|
2018-03-15 16:27:16 -04:00
|
|
|
// GetContext returns ReqInfo if set.
|
|
|
|
func GetContext(ctx context.Context) *ReqInfo {
|
2018-03-14 21:36:54 -04:00
|
|
|
r, ok := ctx.Value(contextLogKey).(*ReqInfo)
|
2018-03-14 15:01:47 -04:00
|
|
|
if ok {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|