Skip to content

Commit

Permalink
Added Looging back in.
Browse files Browse the repository at this point in the history
Added the logger back in which was temp removed while we worked on the config and provider systems.
  • Loading branch information
gdey committed Jul 29, 2016
1 parent e68da4c commit 9066594
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 65 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
language: go

73 changes: 21 additions & 52 deletions cmd/tegola/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,7 @@ func main() {
log.Fatal(err)
}

log.Println("providers", providers)

/*
if err = setupLogger(); err != nil {
log.Fatal(err)
}
*/
initLogger(logFile, logFormat, conf.Webserver.LogFile, conf.Webserver.LogFormat)

// start our webserver
server.Start(conf.Webserver.Port)
Expand Down Expand Up @@ -115,10 +109,10 @@ func initMaps(maps []Map, providers map[string]mvt.Provider) error {

// add our layer to our layers slice
layers = append(layers, server.Layer{
Name: providerLayer[1],
MinZoom: l.MinZoom,
MaxZoom: l.MaxZoom,
Provider: provider,
Name: providerLayer[1],
MinZoom: l.MinZoom,
MaxZoom: l.MaxZoom,
Provider: provider,
DefaultTags: l.DefaultTags,
})
}
Expand All @@ -138,7 +132,6 @@ func initProviders(providers []map[string]interface{}) (map[string]mvt.Provider,

// iterate providers
for _, p := range providers {
log.Printf("provider %v", p)

// lookup our proivder name
n, ok := p["name"]
Expand Down Expand Up @@ -181,52 +174,28 @@ func initProviders(providers []map[string]interface{}) (map[string]mvt.Provider,
return registeredProviders, err
}

/*
func setupLogger() {
func initLogger(cmdFile, cmdFormat, confFile, confFormat string) {
var err error
filename := cmdFile
format := cmdFormat
var file *os.File

// Command line logfile overrides config file.
if logFile != "" {
conf.Webserver.LogFile = logFile
// Need to make sure that the log file exists.
}
if server.DefaultLogFormat != logFormat || conf.Webserver.LogFormat == "" {
conf.Webserver.LogFormat = logFormat
if filename == "" {
filename = confFile
}
if conf.Webserver.LogFile != "" {
if server.LogFile, err = os.OpenFile(logFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666); err != nil {
log.Printf("Unable to open logfile (%v) for writing: %v", logFile, err)
os.Exit(2)
}
if filename == "" {
return
}
if conf.Webserver.LogFormat == "" {
conf.Webserver.LogFormat = server.DefaultLogFormat
if format == "" {
format = confFormat
}

if conf.Webserver.LogFile != "" {
logFile = conf.Webserver.LogFile
}
// Command line logTemplate overrides config file.
if logFormat == "" {
server.LogTemplate = template.New("logfile")
if _, err := server.LogTemplate.Parse(server.DefaultLogFormat); err != nil {
log.Fatal(fmt.Sprintf("Could not parse default template: %v error: %v", server.DefaultLogFormat, err))
}
} else {
server.LogTemplate = conf.Webserver.LogFormat
}
// setup our server log template
server.LogTemplate = template.New("logfile")
if _, err := server.LogTemplate.Parse(conf.Webserver.LogFormat); err != nil {
log.Printf("Could not parse log template: %v error: %v", conf.Webserver.LogFormat, err)
if file, err = os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666); err != nil {
log.Printf("Unable to open logfile (%v) for writing: %v", filename, err)
os.Exit(3)
}
server.L = &server.Logger{
File: file,
Format: format,
}
}
*/
3 changes: 1 addition & 2 deletions server/handle_zxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func handleZXY(w http.ResponseWriter, r *http.Request) {
for _, l := range layers {
// check if layer is within our zoom levels
if l.MinZoom <= tile.Z && l.MaxZoom >= tile.Z {
log.Printf("default tags %+v", l.DefaultTags)
// fetch layer from data provider
mvtLayer, err := l.Provider.MVTLayer(l.Name, tile, l.DefaultTags)
if err != nil {
Expand Down Expand Up @@ -155,7 +154,7 @@ func handleZXY(w http.ResponseWriter, r *http.Request) {
log.Printf("tile z:%v, x:%v, y:%v is rather large - %v", tile.Z, tile.X, tile.Y, len(pbyte))
}
// log the request
Log(logItem{
L.Log(logItem{
X: tile.X,
Y: tile.Y,
Z: tile.Z,
Expand Down
44 changes: 33 additions & 11 deletions server/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import (
"time"
)

var (
LogFile *os.File
LogTemplate *template.Template
)
type Logger struct {
File *os.File
Format string
template *template.Template
skip bool
}

var L *Logger

type logItem struct {
RequestIP string
Expand All @@ -23,21 +27,39 @@ type logItem struct {

const DefaultLogFormat = "{{.Time}}:{{.RequestIP}} —— Tile:{{.Z}}/{{.X}}/{{.Y}}"

func Log(item logItem) {
if LogFile == nil {
func (l *Logger) initTemplate() {
if l == nil || l.template != nil || l.skip {
return
}
if l.Format == "" {
l.Format = DefaultLogFormat
}
// setup our server log template
l.template = template.New("logfile")

if _, err := l.template.Parse(l.Format); err != nil {
log.Printf("Could not parse log template(%v) disabling logging. Error: %v", l.Format, err)
l.skip = true
}
}

func (l *Logger) Log(item logItem) {
l.initTemplate()
if l == nil || l.File == nil || l.skip {
return
}

if item.Time.IsZero() {
item.Time = time.Now()
}

var l string
lbuf := bytes.NewBufferString(l)
var lstr string
lbuf := bytes.NewBufferString(lstr)

if err := LogTemplate.Execute(lbuf, item); err != nil {
if err := l.template.Execute(lbuf, item); err != nil {
// Don't care about the error.
log.Println("Error writing to log file", err)
log.Println("Error writing to log file; disabling logging.", err)
l.skip = true
return
}
b := lbuf.Bytes()
Expand All @@ -48,5 +70,5 @@ func Log(item logItem) {
}

// Don't care about the error.
LogFile.Write(b)
l.File.Write(b)
}

0 comments on commit 9066594

Please sign in to comment.