Esempio n. 1
0
func (l barkLogrusLogger) WithFields(logFields LogFields) Logger {
	if logFields == nil {
		return l
	}

	return newBarkLogrusLogger(l.logrusLoggerOrEntry.WithFields(logrus.Fields(logFields.Fields())))
}
Esempio n. 2
0
func (c *Cache) calculateStats(reportingDuration time.Duration) {
	c.lock.Lock()
	defer c.lock.Unlock()

	ratio := 100
	if c.hits+c.misses != 0 {
		ratio = 100 * c.hits / (c.hits + c.misses)
	}

	c.stats = map[string]interface{}{
		"type":                     "metric",
		"metric_name":              "cachestatus",
		"cache_entries":            c.lruBackend.Len(),
		"cache_size_bytes":         c.currentSizeBytes,
		"cache_reporting_duration": reportingDuration,
		"cache_hits":               c.hits,
		"cache_misses":             c.misses,
		"cache_hit_ratio":          ratio,
	}

	c.hits = 0
	c.misses = 0
	logging.Logger.
		WithFields(logrus.Fields(c.stats)).
		Infof("cache status #%v, %vbytes, %v%% hits", c.lruBackend.Len(), c.currentSizeBytes, ratio)
}
Esempio n. 3
0
func (logger *Logger) Debug(message interface{}, fields ...Fields) {
	var log logrus.FieldLogger = logger.Logger
	if len(fields) > 0 {
		log = log.WithFields(logrus.Fields(fields[0]))
	}

	log.Debug(message)
}
Esempio n. 4
0
func (l *LogrusLogger) ParamsWithFields(params interface{}, f map[string]interface{}) {
	var pstr string
	pb, err := json.Marshal(params)
	if err != nil {
		pstr = "json marshal error"
	} else {
		pstr = string(pb)
	}

	f["params"] = pstr
	l.Logger.WithFields(logrus.Fields(f)).Info("params_log")
}
Esempio n. 5
0
func (l Logger) WithFields(fields map[string]interface{}) Entry {
	if fields == nil {
		fields = make(map[string]interface{})
	}

	_, file, line, _ := runtime.Caller(2)
	fields["*TIME"] = getTime()
	fields[".FILE"] = trimFile(file)
	fields[".LINE"] = line
	return Entry{
		l.logger.WithFields(logrus.Fields(fields)),
	}
}
Esempio n. 6
0
// WithFields returns an advanced logger with pre-set fields.
func (l *Logger) WithFields(fields ...interface{}) loggers.Advanced {
	f := make(map[string]interface{}, len(fields)/2)
	var key, value interface{}
	for i := 0; i+1 < len(fields); i = i + 2 {
		key = fields[i]
		value = fields[i+1]
		if s, ok := key.(string); ok {
			f[s] = value
		} else if s, ok := key.(fmt.Stringer); ok {
			f[s.String()] = value
		}
	}

	return l.Logger.WithFields(logrus.Fields(f))
}
func TestGetUserFromString(t *testing.T) {
	assert := assert.New(t)

	tests := []struct {
		data        map[string]interface{}
		expected    bool
		description string
	}{
		{map[string]interface{}{
			"user_name":  "name",
			"user_email": "*****@*****.**",
			"user_id":    "A0001",
			"user_ip":    "0.0.0.0",
		}, true, "valid user"},
		{map[string]interface{}{"user_name": "name"}, true, "valid user"},
		{map[string]interface{}{"user_email": "*****@*****.**"}, true, "valid user"},
		{map[string]interface{}{"user_id": "A0001"}, true, "valid user"},
		{map[string]interface{}{"user_ip": "0.0.0.0"}, true, "valid user"},
		{map[string]interface{}{"user_name": ""}, false, "invalid user: empty user_name"},
		{map[string]interface{}{"user_email": ""}, false, "invalid user: empty user_email"},
		{map[string]interface{}{"user_id": ""}, false, "invalid user: empty user_id"},
		{map[string]interface{}{"user_ip": ""}, false, "invalid user: empty user_ip"},
		{map[string]interface{}{
			"user_name":  1,
			"user_email": true,
			"user_id":    errors.New("user_id"),
			"user_ip":    "",
		}, false, "invalid types"},
	}

	for _, tt := range tests {
		target := fmt.Sprintf("%+v", tt)

		fields := logrus.Fields(tt.data)

		df := newDataField(fields)
		user, ok := df.getUser()
		assert.Equal(tt.expected, ok, target)
		if ok {
			assert.IsType(&raven.User{}, user, target)
		}
	}
}
Esempio n. 8
0
File: error.go Progetto: snikch/api
// RespondWithError will return an error response with the appropriate message,
// and status codes set.
func RespondWithError(w http.ResponseWriter, r *http.Request, err error) {
	isPublicError := false
	errorResponse := APIError{
		Error: err.Error(),
	}
	code := http.StatusInternalServerError
	if statusErr, ok := err.(StatusError); ok {
		// If we get a status code, this error can be considered a public error.
		isPublicError = true
		code = statusErr.StatusCode()
	}

	if descriptiveErr, ok := err.(DescriptiveError); ok {
		errorResponse.Description = descriptiveErr.ErrorDescription()
	}

	if annotatedErr, ok := err.(AnnotatedError); ok {
		errorResponse.Fields = annotatedErr.ErrorFields()
	}

	w.WriteHeader(code)

	// Now log some information about the failure.
	logData := map[string]interface{}{}
	if structuredLogErr, ok := err.(StructuredLogsError); ok {
		for key, value := range structuredLogErr.LogFields() {
			logData[key] = value
		}
	}
	logData["status"] = code

	if !isPublicError {
		logData["original_error"] = err.Error()
		err = fail.NewPrivate(err)
		errorResponse.Error = err.Error()
	}

	body := DefaultRenderer.RenderError(errorResponse)
	w.Write(body)

	log.WithError(err).WithFields(logrus.Fields(logData)).Error("Returning error response")

}
Esempio n. 9
0
// Purge Entries with a specific hash
func (c *Cache) PurgeEntries(keys []string) {
	purged := 0
	purgedKeys := []string{}
	for _, key := range keys {
		c.lock.RLock()
		_, found := c.lruBackend.Peek(key)
		c.lock.RUnlock()

		if found {
			c.lock.Lock()
			c.lruBackend.Remove(key)
			c.lock.Unlock()
			purged++
			purgedKeys = append(purgedKeys, key)
		}
	}
	logging.Logger.
		WithFields(logrus.Fields(c.stats)).
		Infof("Following cache entries become purged: %v", c.PurgedKeysAsString(keys))
}
Esempio n. 10
0
func TrackEvent(event string, params map[string]interface{}) {
	log := logrus.WithFields(logrus.Fields{"ns": "api.helpers", "at": "TrackEvent"})

	if params == nil {
		params = map[string]interface{}{}
	}

	params["client_id"] = os.Getenv("CLIENT_ID")
	params["rack"] = os.Getenv("RACK")
	params["release"] = os.Getenv("RELEASE")

	userId := RackId()

	log.WithFields(logrus.Fields{"event": event, "user_id": userId}).WithFields(logrus.Fields(params)).Info()

	segment.Track(&analytics.Track{
		Event:      event,
		UserId:     userId,
		Properties: params,
	})
}
Esempio n. 11
0
func (log *Logger) newLog(level logrus.Level, data map[string]interface{}) {
	log.rotate()

	data["_ts_"] = utils.GetNowMillisecond()
	fields := logrus.Fields(data)

	switch level {
	case LOG_LEVEL_DEBUG:
		log.WithFields(fields).Debug("")
	case LOG_LEVEL_INFO:
		log.WithFields(fields).Info("")
	case LOG_LEVEL_WARN:
		log.WithFields(fields).Warn("")
	case LOG_LEVEL_ERROR:
		log.WithFields(fields).Error("")
	case LOG_LEVEL_FATAL:
		log.WithFields(fields).Fatal("")
	case LOG_LEVEL_PANIC:
		log.WithFields(fields).Panic("")
	default:
		log.WithFields(fields).Info("")
	}
}
Esempio n. 12
0
// PurgeOldEntries removes all entries which are out of their ttl
func (c *Cache) PurgeOldEntries() {
	c.lock.RLock()
	keys := c.lruBackend.Keys()
	c.lock.RUnlock()
	purged := 0
	for _, key := range keys {
		c.lock.RLock()
		e, found := c.lruBackend.Peek(key)
		c.lock.RUnlock()

		if found {
			entry := e.(*CacheEntry)
			if time.Since(entry.fetchTime) > c.maxAge {
				c.lock.Lock()
				c.lruBackend.Remove(key)
				c.lock.Unlock()
				purged++
			}
		}
	}
	logging.Logger.
		WithFields(logrus.Fields(c.stats)).
		Infof("purged %v out of %v cache entries", purged, len(keys))
}
Esempio n. 13
0
func (l *LogrusLogger) ErrorWithFields(e error, f map[string]interface{}) {
	f["msg"] = e.Error()
	logrusF := logrus.Fields(f)
	l.Logger.WithFields(logrusF).Error("error")
}
Esempio n. 14
0
func (e Entry) WithFields(fields map[string]interface{}) Entry {
	return Entry{
		e.Entry.WithFields(logrus.Fields(fields)),
	}
}
Esempio n. 15
0
func (l *LogrusLogger) InfoWithFields(msg string, f map[string]interface{}) {
	logrusF := logrus.Fields(f)
	l.Logger.WithFields(logrusF).Info(msg)
}
Esempio n. 16
0
// GetLoggerWithFields returns a logger instance with the specified fields
// without affecting the context. Extra specified keys will be resolved from
// the context.
func GetLoggerWithFields(ctx Context, fields map[string]interface{}, keys ...interface{}) Logger {
	return getLogrusLogger(ctx, keys...).WithFields(logrus.Fields(fields))
}
Esempio n. 17
0
func (f Fields) Fields() logrus.Fields {
	return logrus.Fields(f)
}
Esempio n. 18
0
// WithFields wraps logrus
func (e *LogEntry) WithFields(fields LogFields) *LogEntry {
	return &LogEntry{e.Entry.WithFields(logrus.Fields(fields))}
}
Esempio n. 19
0
// WithFields wraps logrus
func (l *Logger) WithFields(fields LogFields) *LogEntry {
	return &LogEntry{l.Logger.WithFields(logrus.Fields(fields))}
}
Esempio n. 20
0
// WithFields creates a child logger annotated with the provided key value
// pairs.
func (e *Entry) WithFields(fields F) *Entry {
	return &Entry{*e.Entry.WithFields(logrus.Fields(fields))}
}
Esempio n. 21
0
// WithFields creates an entry from the standard logger and adds multiple
// fields to it. This is simply a helper for `WithField`, invoking it
// once for each field.
//
// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
// or Panic on the Entry it returns.
func WithFields(fields Fields) *logrus.Entry {
	return Logger.WithFields(logrus.Fields(fields))
}