package logger

import (
	"os"
	"strconv"

	"github.com/Sirupsen/logrus"
	"github.com/polds/logrus/hooks/papertrail"
)

var __l *logrus.Logger

// Logger returns an instance of
// a logger or creates a new one.
func Logger() *logrus.Logger {
	if __l == nil {
		__l = NewLogger()
	}

	return __l
}

// NewLogger creates a new instances of a
// logrus logger and returns the instance.
func NewLogger() *logrus.Logger {
	app := os.Getenv("APPNAME")
	if app == "" {
		app, _ = os.Hostname()
		os.Setenv("APPNAME", app)
	}

	host := os.Getenv("PAPERTRAIL_HOST")
	port, _ := strconv.Atoi(os.Getenv("PAPERTRAIL_PORT"))

	log := logrus.New()
	hook, err := logrus_papertrail.NewPapertrailHook(host, port, app)
	hook.UseHostname()

	// Register the PaperTrail hook
	if err == nil {
		log.Hooks.Add(hook)
	}

	return log
}

package logger

import (
	"os"
	"strconv"

	"github.com/Sirupsen/logrus"
	"github.com/polds/logrus/hooks/papertrail"
)

var __l *logrus.Logger

type Config struct {
	Appname string
	Host    string
	Port    int

	*logrus.Logger
}

// Logger returns an instance of
// a logger or creates a new one.
func Logger() *logrus.Logger {
	if __l == nil {
		__l = NewLogger()
	}

	return __l
}

// New creates a new instance of a
// logger based on provided config data
func New(config Config) Config {
	config.Logger = logrus.New()
	hook, err := logrus_papertrail.NewPapertrailHook(config.Host, config.Port, config.Appname)
	hook.UseHostname()

	// Register the PaperTrail hook
	if err == nil {
		config.Logger.Hooks.Add(hook)
	}

	return config
}

// DefaultConfig makes certain assumptions about your environment variables
// and can be used to create a new basic instance.
func DefaultConfig() Config {
	app := os.Getenv("APPNAME")
	if app == "" {
		app, _ = os.Hostname()
		os.Setenv("APPNAME", app)
	}

	port, _ := strconv.Atoi(os.Getenv("PAPERTRAIL_PORT"))
	return Config{
		Appname: os.Getenv("APPNAME"),
		Host:    os.Getenv("PAPERTRAIL_HOST"),
		Port:    port,
	}
}

// @TODO polds deprecate this function
func NewLogger() *logrus.Logger {
	return New(DefaultConfig()).Logger
}

Add a "New()" function that allows for custom conf
Add a  "New()" function that allows for custom conf

Signed-off-by: Peter Olds <f516c8e349fcaee4098dbef1ab3f4b8dc00e321e@kyanicorp.com>
