Beispiel #1
0
func (m *Logger) ServeHTTP(w http.ResponseWriter, r *http.Request, n http.HandlerFunc) {
	remoteAddr := r.RemoteAddr
	if realIP := r.Header.Get("X-Real-IP"); realIP != "" {
		remoteAddr = realIP
	}

	f := logrus.Fields{}
	f["request"] = r.URL.Path
	f["method"] = r.Method
	f["remote"] = remoteAddr
	logrus.NewEntry(m.Logger).WithFields(f).Debug("new request accepted")

	start := time.Now()
	n(w, r)

	status := w.(negroni.ResponseWriter).Status()

	f["status"] = status
	f["elapsed"] = time.Since(start)

	entry := logrus.NewEntry(m.Logger).WithFields(f)
	if status != 200 {
		entry.Warning("completed handling request, with errors")
		return
	}

	entry.Info("completed handling request")
}
Beispiel #2
0
func (ctx *Context) logger(req *http.Request) *logrus.Entry {
	log := logrus.NewEntry(ctx.log)
	if reqID := requestID(req); reqID != "" {
		log = log.WithField("request_id", reqID)
	}
	return log
}
Beispiel #3
0
func (l *StructuredLogger) NewLogEntry(r *http.Request) middleware.LogEntry {
	entry := &StructuredLoggerEntry{logger: logrus.NewEntry(l.logger)}
	logFields := logrus.Fields{}

	logFields["ts"] = time.Now().UTC().Format(time.RFC1123)

	if reqID := middleware.GetReqID(r.Context()); reqID != "" {
		logFields["req_id"] = reqID
	}

	scheme := "http"
	if r.TLS != nil {
		scheme = "https"
	}
	logFields["http_scheme"] = scheme
	logFields["http_proto"] = r.Proto
	logFields["http_method"] = r.Method

	logFields["remote_addr"] = r.RemoteAddr
	logFields["user_agent"] = r.UserAgent()

	logFields["uri"] = fmt.Sprintf("%s://%s%s", scheme, r.Host, r.RequestURI)

	entry.logger = entry.logger.WithFields(logFields)

	entry.logger.Infoln("request started")

	return entry
}
Beispiel #4
0
func TestFetchWishlist(t *testing.T) {
	log := logrus.NewEntry(logrus.New())
	wishlister, _ := NewFakeWishlister([]byte{}, log)

	conf := WishlistConfig{
		Wishlisters:           []Wishlister{wishlister},
		ShowDefaultQualities:  []Quality{Quality480p, Quality1080p},
		MovieDefaultQualities: []Quality{Quality1080p, Quality720p},
	}

	wl := NewWishlist(conf, log)

	if err := wl.Fetch(); err != nil {
		t.Fatalf("Expected no error, got %q", err)
	}

	// Test movies
	movies := wl.Movies
	if !reflect.DeepEqual(movies, expectedWishedMoviesWithQualities) {
		t.Errorf("Expected %#v, got %#v", expectedWishedMovies, movies)
	}

	shows := wl.Shows
	if !reflect.DeepEqual(shows, expectedWishedShowsWithQualities) {
		t.Errorf("Expected %#v, got %#v", expectedWishedShows, shows)
	}
}
Beispiel #5
0
// NewLogger returns a log configured with the input parameters.
func NewLogger(level, writer string, maxSize, maxAge, maxBackups int) *logrus.Entry {
	base := logrus.New()

	switch writer {
	case "stdout":
		base.Out = os.Stdout
	case "stderr":
		base.Out = os.Stderr
	case "":
		base.Out = ioutil.Discard
	default:
		base.Out = &lumberjack.Logger{
			Filename:   writer,
			MaxSize:    maxSize,
			MaxAge:     maxAge,
			MaxBackups: maxBackups,
		}
	}

	logrusLevel, err := logrus.ParseLevel(level)
	// if provided level is not supported, default to Info level
	if err != nil {
		base.Error(err)
		logrusLevel = logrus.InfoLevel
	}
	base.Level = logrusLevel

	log := logrus.NewEntry(base)
	return log
}
Beispiel #6
0
func TestLogrusWithJsonFormatter(t *testing.T) {
	p := &Person{
		Name:  "Bruce",
		Alias: "Batman",
		Hideout: &Hideout{
			Name:        "JLU Tower",
			DimensionId: 52,
		},
	}

	log.SetFormatter(newJsonFormatter())

	entry := log.NewEntry(log.StandardLogger())
	entry.Message = "the dark knight"
	entry.Data = log.Fields{"hero": p}
	s, _ := entry.String()

	if !strings.Contains(s, `"hero.name":"Bruce"`) {
		t.Fatalf(`missing "hero.name":"Bruce"`)
	}

	if !strings.Contains(s, `"hero.alias":"Batman"`) {
		t.Fatalf(`missing "hero.alias":"Batman"`)
	}

	if !strings.Contains(s, `"hero.hideout.name":"JLU Tower"`) {
		t.Fatalf(`missing "hero.hideout.name":"JLU Tower"`)
	}

	if !strings.Contains(s, `"hero.hideout.dimensionId":52`) {
		t.Fatalf(`missing "hero.hideout.dimensionId":52`)
	}
}
Beispiel #7
0
// initialize a repo with keys, so they can be rotated
func setUpRepo(t *testing.T, tempBaseDir, gun string, ret notary.PassRetriever) (
	*httptest.Server, map[string]string) {

	// Set up server
	ctx := context.WithValue(
		context.Background(), "metaStore", storage.NewMemStorage())

	// Do not pass one of the const KeyAlgorithms here as the value! Passing a
	// string is in itself good test that we are handling it correctly as we
	// will be receiving a string from the configuration.
	ctx = context.WithValue(ctx, "keyAlgorithm", "ecdsa")

	// Eat the logs instead of spewing them out
	l := logrus.New()
	l.Out = bytes.NewBuffer(nil)
	ctx = ctxu.WithLogger(ctx, logrus.NewEntry(l))

	cryptoService := cryptoservice.NewCryptoService(trustmanager.NewKeyMemoryStore(ret))
	ts := httptest.NewServer(server.RootHandler(nil, ctx, cryptoService, nil, nil, nil))

	repo, err := client.NewNotaryRepository(
		tempBaseDir, gun, ts.URL, http.DefaultTransport, ret, trustpinning.TrustPinConfig{})
	require.NoError(t, err, "error creating repo: %s", err)

	rootPubKey, err := repo.CryptoService.Create("root", "", data.ECDSAKey)
	require.NoError(t, err, "error generating root key: %s", err)

	err = repo.Initialize(rootPubKey.ID())
	require.NoError(t, err)

	return ts, repo.CryptoService.ListAllKeys()
}
Beispiel #8
0
// LogRPCWithFields will feed any request context into a logrus Entry.
func LogRPCWithFields(log *logrus.Logger, ctx context.Context) *logrus.Entry {
	md, ok := metadata.FromContext(ctx)
	if !ok {
		return logrus.NewEntry(log)
	}
	return log.WithFields(MetadataToFields(md))
}
Beispiel #9
0
func TestSetupBolt(t *testing.T) {
	log := logrus.NewEntry(&logrus.Logger{})
	err := SetupBolt(path, log)
	if err != nil {
		t.Errorf("Failed to setup bolt: %s", err)
	}
	defer os.Remove(path)

	if err := DB.Close(); err != nil {
		t.Errorf("Failed to close bolt: %s", err)
	}

	// check if meta bucket exists
	db, err := bolt.Open(path, 0600, nil)
	if err != nil {
		t.Errorf("Failed to open bolt: %s", err)
	}

	db.View(func(tx *bolt.Tx) error {
		bkt := tx.Bucket([]byte(metabucket))
		if bkt == nil {
			t.Errorf("Meta bucket nof found")
		}
		return nil
	})

}
Beispiel #10
0
// Spawn initializes the HTTP component
func (httpsender *HTTPSender) Spawn(id int) utils.Composer {
	s := *httpsender
	s.id = id

	if httpsender.Config.Logger == nil {
		s.logger = logrus.NewEntry(logrus.New())
		s.logger.Logger.Out = ioutil.Discard
	} else {
		s.logger = httpsender.Config.Logger.WithFields(logrus.Fields{
			"worker": id,
		})
	}

	if httpsender.Debug {
		s.logger.Logger.Level = logrus.DebugLevel
	}

	s.logger.Debugf("Spawning worker")

	if govalidator.IsURL(s.URL) {
		s.Client = new(http.Client)

		if httpsender.Config.Insecure {
			s.Client.Transport = &http.Transport{
				TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			}
		}
	} else {
		s.err = errors.New("Invalid URL")
	}

	return &s
}
Beispiel #11
0
// GetLogrusLogger returns the logrus logger for the context. If one more keys
// are provided, they will be resolved on the context and included in the
// logger. Only use this function if specific logrus functionality is
// required.
func getLogrusLogger(ctx Context, keys ...interface{}) *logrus.Entry {
	var logger *logrus.Entry

	// Get a logger, if it is present.
	loggerInterface := ctx.Value("logger")
	if loggerInterface != nil {
		if lgr, ok := loggerInterface.(*logrus.Entry); ok {
			logger = lgr
		}
	}

	if logger == nil {
		// If no logger is found, just return the standard logger.
		logger = logrus.NewEntry(logrus.StandardLogger())
	}

	fields := logrus.Fields{}

	for _, key := range keys {
		v := ctx.Value(key)
		if v != nil {
			fields[fmt.Sprint(key)] = v
		}
	}

	return logger.WithFields(fields)
}
Beispiel #12
0
func dumpConfigToLog() {
	l := log.NewEntry(log.StandardLogger())

	for k, v := range config.AllSettings() {
		l = l.WithField(k, v)
	}
	l.Debug("running with configuration")
}
func TestWrite(t *testing.T) {
	entry := logrus.NewEntry(logrus.New())
	entry.Message = "foobar"
	formatter := MachineFormatter{}
	bytes, err := formatter.Format(entry)
	assert.Nil(t, err)
	assert.Equal(t, string(bytes[:]), "foobar\n")
}
Beispiel #14
0
func TestGetChangelogCache(t *testing.T) {
	const servername = "server1"
	pack := models.PackageInfo{
		Name:       "apt",
		Version:    "1.0.0",
		NewVersion: "1.0.1",
	}
	var meta = cache.Meta{
		Name: servername,
		Distro: config.Distro{
			Family:  "ubuntu",
			Release: "16.04",
		},
		Packs: []models.PackageInfo{pack},
	}

	const path = "/tmp/vuls-test-cache-11111111.db"
	log := logrus.NewEntry(&logrus.Logger{})
	if err := cache.SetupBolt(path, log); err != nil {
		t.Errorf("Failed to setup bolt: %s", err)
	}
	defer os.Remove(path)

	if err := cache.DB.EnsureBuckets(meta); err != nil {
		t.Errorf("Failed to ensure buckets: %s", err)
	}

	d := newDebian(config.ServerInfo{})
	actual := d.getChangelogCache(&meta, pack)
	if actual != "" {
		t.Errorf("Failed to get empty stirng from cache:")
	}

	clog := "changelog-text"
	if err := cache.DB.PutChangelog(servername, "apt", clog); err != nil {
		t.Errorf("Failed to put changelog: %s", err)
	}

	actual = d.getChangelogCache(&meta, pack)
	if actual != clog {
		t.Errorf("Failed to get changelog from cache: %s", actual)
	}

	// increment a version of the pack
	pack.NewVersion = "1.0.2"
	actual = d.getChangelogCache(&meta, pack)
	if actual != "" {
		t.Errorf("The changelog is not invalidated: %s", actual)
	}

	// change a name of the pack
	pack.Name = "bash"
	actual = d.getChangelogCache(&meta, pack)
	if actual != "" {
		t.Errorf("The changelog is not invalidated: %s", actual)
	}
}
Beispiel #15
0
// New creates a new logger according to horizon specifications.
func New() (result *logrus.Entry, m *Metrics) {
	m = NewMetrics()
	l := logrus.New()
	l.Level = logrus.WarnLevel
	l.Hooks.Add(m)

	result = logrus.NewEntry(l)
	return
}
Beispiel #16
0
// init the app by reading the configuration files
func (a *App) init() error {
	log := logrus.NewEntry(a.logger).WithField("function", "app_init")
	log.Debug("loading app configuration")

	config, err := configuration.LoadConfigFile(a.configPath, log)
	if err != nil {
		return err
	}

	library := library.New(config.File, config.Movie, config.Show, config.Library)

	// Build the library index
	if err := library.RebuildIndex(log); err != nil {
		log.WithField("function", "rebuild_index").Error(err)
	}

	// Add the organizer
	a.subApps = []subapp.App{organizer.New(config, library)}

	if config.Downloader.Enabled {
		// Add the downloader
		a.subApps = append(a.subApps, downloader.New(config, library))

		if config.Downloader.Cleaner.Enabled {
			// Add the cleaner
			a.subApps = append(a.subApps, cleaner.New(config))
		}
	}

	// Only run the HTTP server if specified
	if config.HTTPServer.Enable {
		// Read the config of the token manager
		var tokenManager *token.Manager
		if _, err := os.Stat(a.tokenConfigPath); err == nil {
			log.Debug("loading token manager configuration")

			file, err := os.Open(a.tokenConfigPath)
			defer file.Close()
			if err != nil {
				return err
			}

			tokenManager, err = token.LoadFromYaml(file)
			if err != nil {
				return err
			}
			log.Debug("token manager configuration loaded")
		}

		// Add the http server
		a.subApps = append(a.subApps, server.New(config, library, tokenManager))
	}

	log.Debug("app configuration loaded")

	return nil
}
Beispiel #17
0
// prepareEntry will parse the input params and build a logrus.Entry from them.
// It will return the *logrus.Entry and the message to be printed
func prepareEntry(m []interface{}) (*logrus.Entry, interface{}) {
	var msg interface{}
	data := logrus.Fields{}
	if len(m)%2 != 0 {
		msg = m[0]
		m = m[1:]
	}
	for i := 0; i < len(m); i = i + 2 {
		data[m[i].(string)] = m[i+1]
	}
	return logrus.NewEntry(DefaultLogger).WithFields(data), msg
}
Beispiel #18
0
func InitLogger(logLevel string, node string) {
	formattedLogger := logrus.New()
	formattedLogger.Formatter = &logrus.TextFormatter{FullTimestamp: true}

	level, err := logrus.ParseLevel(logLevel)
	if err != nil {
		logrus.WithError(err).Error("Error parsing log level, using: info")
		level = logrus.InfoLevel
	}

	formattedLogger.Level = level
	log = logrus.NewEntry(formattedLogger).WithField("node", node)
}
Beispiel #19
0
func setupServerHandler(metaStore storage.MetaStore) http.Handler {
	ctx := context.WithValue(context.Background(), "metaStore", metaStore)

	ctx = context.WithValue(ctx, "keyAlgorithm", data.ECDSAKey)

	// Eat the logs instead of spewing them out
	var b bytes.Buffer
	l := logrus.New()
	l.Out = &b
	ctx = ctxu.WithLogger(ctx, logrus.NewEntry(l))

	cryptoService := cryptoservice.NewCryptoService(trustmanager.NewKeyMemoryStore(passphrase.ConstantRetriever("pass")))
	return server.RootHandler(nil, ctx, cryptoService, nil, nil, nil)
}
Beispiel #20
0
func Debug(ctx context.Context, fields map[string]interface{}, format string, args ...interface{}) {
	if logger.Level >= log.DebugLevel {
		entry := log.NewEntry(logger)

		if ctx != nil {
			entry = entry.WithField("req_id", extractRequestID(ctx))
		}

		if len(args) > 0 {
			entry.WithFields(fields).Debugf(format, args...)
		} else {
			entry.WithFields(fields).Debugln(format)
		}
	}
}
func assertFormatter(
	t *testing.T,
	expected string,
	logLevel logrus.Level,
	msg string,
) {
	entry := logrus.NewEntry(logrus.StandardLogger())
	entry.Level = logLevel
	entry.Message = msg
	msgBytes, _ := newFormatter(false).Format(entry)
	assert.Equal(t,
		expected,
		string(msgBytes),
	)
}
Beispiel #22
0
func getSSHLogger(log ...*logrus.Entry) *logrus.Entry {
	if len(log) == 0 {
		level := logrus.InfoLevel
		if conf.Conf.Debug == true {
			level = logrus.DebugLevel
		}
		l := &logrus.Logger{
			Out:       os.Stderr,
			Formatter: new(logrus.TextFormatter),
			Hooks:     make(logrus.LevelHooks),
			Level:     level,
		}
		return logrus.NewEntry(l)
	}
	return log[0]
}
Beispiel #23
0
func NewLogrusLogger(conf *config.Config) (logrusLogger *LogrusLogger) {
	var logger = logrus.New()

	formater := &logrus.TextFormatter{
		DisableColors:   true,
		FullTimestamp:   true,
		TimestampFormat: "2006-01-02 15:04:05",
	}
	logger.Formatter = formater

	if conf == nil {
		return
	}

	if conf.Logger.Color {
		formater.ForceColors = true
		formater.DisableColors = false
	} else {
		formater.ForceColors = false
		formater.DisableColors = true
	}

	level, err := logrus.ParseLevel(conf.Logger.Level)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	logger.Level = level

	if conf.Logger.LogFile != "" {
		f, err := os.OpenFile(conf.Logger.LogFile, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
		logger.Out = f
	}

	logrusLogger = &LogrusLogger{logrus.NewEntry(logger)}

	// if pc, file, line, ok := runtime.Caller(1); ok {
	// 	fName := runtime.FuncForPC(pc).Name()
	// 	logrusLogger.Entry = logger.WithField("file", file).WithField("line", line).WithField("func", fName)
	// }

	return
}
Beispiel #24
0
// makes a testing notary-server
func setupServer() *httptest.Server {
	// Set up server
	ctx := context.WithValue(
		context.Background(), "metaStore", storage.NewMemStorage())

	ctx = context.WithValue(ctx, "keyAlgorithm", data.ECDSAKey)

	// Eat the logs instead of spewing them out
	var b bytes.Buffer
	l := logrus.New()
	l.Out = &b
	ctx = ctxu.WithLogger(ctx, logrus.NewEntry(l))

	cryptoService := cryptoservice.NewCryptoService(
		"", trustmanager.NewKeyMemoryStore(retriever))
	return httptest.NewServer(server.RootHandler(nil, ctx, cryptoService))
}
Beispiel #25
0
func TestTmdbFailedToGetDetails(t *testing.T) {
	m := &polochon.Movie{Title: "The Matrix", ImdbID: "tt0133093"}

	tmdbSearchByImdbID = func(t *tmdb.TMDb, id, source string, options map[string]string) (*tmdb.FindResults, error) {
		return &tmdb.FindResults{}, nil
	}

	tmdbSearchMovie = func(t *tmdb.TMDb, title string, options map[string]string) (*tmdb.MovieSearchResults, error) {
		return &tmdb.MovieSearchResults{Results: []tmdb.MovieShort{}}, nil
	}

	log := logrus.NewEntry(logrus.New())

	err := mockTmdb.GetDetails(m, log)
	if err != ErrFailedToGetDetails {
		log.Fatalf("Got %q, expected %q", err, ErrFailedToGetDetails)
	}
}
Beispiel #26
0
func (dh DeploymentHook) Fire(entry *logrus.Entry) error {
	if !dh.logManager.loggingEnabled {
		return nil
	}

	// customize log message to contain only message, level and time
	dLog := logrus.NewEntry(entry.Logger)
	dLog.Message = entry.Message
	dLog.Level = entry.Level
	dLog.Time = entry.Time

	message, err := dh.formater.Format(dLog)
	if err != nil {
		return err
	}

	err = dh.logManager.WriteLog(message)
	return err
}
Beispiel #27
0
func (m *Middleware) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	if m.Before == nil {
		m.Before = DefaultBefore
	}

	if m.After == nil {
		m.After = DefaultAfter
	}

	for _, u := range m.excludeURLs {
		if r.URL.Path == u {
			return
		}
	}

	start := m.clock.Now()

	// Try to get the real IP
	remoteAddr := r.RemoteAddr
	if realIP := r.Header.Get("X-Real-IP"); realIP != "" {
		remoteAddr = realIP
	}

	entry := logrus.NewEntry(m.Logger)

	if reqID := r.Header.Get("X-Request-Id"); reqID != "" {
		entry = entry.WithField("request_id", reqID)
	}

	entry = m.Before(entry, r, remoteAddr)

	if m.logStarting {
		entry.Info("started handling request")
	}

	next(rw, r)

	latency := m.clock.Since(start)
	res := rw.(negroni.ResponseWriter)

	m.After(entry, res, latency, m.Name).Info("completed handling request")
}
Beispiel #28
0
func fullTestServer(t *testing.T) *httptest.Server {
	// Set up server
	ctx := context.WithValue(
		context.Background(), "metaStore", storage.NewMemStorage())

	// Do not pass one of the const KeyAlgorithms here as the value! Passing a
	// string is in itself good test that we are handling it correctly as we
	// will be receiving a string from the configuration.
	ctx = context.WithValue(ctx, "keyAlgorithm", "ecdsa")

	// Eat the logs instead of spewing them out
	var b bytes.Buffer
	l := logrus.New()
	l.Out = &b
	ctx = ctxu.WithLogger(ctx, logrus.NewEntry(l))

	cryptoService := cryptoservice.NewCryptoService(
		"", trustmanager.NewKeyMemoryStore(passphraseRetriever))
	return httptest.NewServer(server.RootHandler(nil, ctx, cryptoService))
}
Beispiel #29
0
func Warn(ctx context.Context, fields map[string]interface{}, format string, args ...interface{}) {
	if logger.Level >= log.WarnLevel {
		entry := log.NewEntry(logger)

		file, _, fName, err := extractCallerDetails()
		if err != nil {
			entry = log.WithField("file", file).WithField("func", fName)
		}

		if ctx != nil {
			entry = entry.WithField("req_id", extractRequestID(ctx))
		}

		if len(args) > 0 {
			entry.WithFields(fields).Warnf(format, args...)
		} else {
			entry.WithFields(fields).Warnln(format)
		}
	}
}
Beispiel #30
0
func TestEnsureBuckets(t *testing.T) {
	log := logrus.NewEntry(&logrus.Logger{})
	if err := SetupBolt(path, log); err != nil {
		t.Errorf("Failed to setup bolt: %s", err)
	}
	if err := DB.EnsureBuckets(meta); err != nil {
		t.Errorf("Failed to ensure buckets: %s", err)
	}
	defer os.Remove(path)

	m, found, err := DB.GetMeta(servername)
	if err != nil {
		t.Errorf("Failed to get meta: %s", err)
	}
	if !found {
		t.Errorf("Not Found in meta")
	}
	if meta.Name != m.Name || meta.Distro != m.Distro {
		t.Errorf("expected %v, actual %v", meta, m)
	}
	if !reflect.DeepEqual(meta.Packs, m.Packs) {
		t.Errorf("expected %v, actual %v", meta.Packs, m.Packs)
	}
	if err := DB.Close(); err != nil {
		t.Errorf("Failed to close bolt: %s", err)
	}

	db, err := bolt.Open(path, 0600, nil)
	if err != nil {
		t.Errorf("Failed to open bolt: %s", err)
	}
	db.View(func(tx *bolt.Tx) error {
		bkt := tx.Bucket([]byte(servername))
		if bkt == nil {
			t.Errorf("Meta bucket nof found")
		}
		return nil
	})
}