Merge pull request #161 from vishh/cleanup_logs

Use glog instead of 'log' library.
This commit is contained in:
Victor Marmol 2014-08-07 11:49:23 -07:00
commit 0311058006
12 changed files with 53 additions and 50 deletions

View File

@ -20,12 +20,12 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"path"
"strings"
"time"
"github.com/golang/glog"
"github.com/google/cadvisor/info"
"github.com/google/cadvisor/manager"
)
@ -41,8 +41,8 @@ const (
)
var supportedApiVersions map[string]struct{} = map[string]struct{}{
version1_0: struct{}{},
version1_1: struct{}{},
version1_0: {},
version1_1: {},
}
func RegisterHandlers(m manager.Manager) error {
@ -92,7 +92,7 @@ func handleRequest(m manager.Manager, w http.ResponseWriter, r *http.Request) er
switch {
case requestType == machineApi:
log.Printf("Api - Machine")
glog.V(2).Infof("Api - Machine")
// Get the MachineInfo
machineInfo, err := m.GetMachineInfo()
@ -109,7 +109,7 @@ func handleRequest(m manager.Manager, w http.ResponseWriter, r *http.Request) er
// The container name is the path after the requestType.
containerName := path.Join("/", strings.Join(requestArgs, "/"))
log.Printf("Api - Container(%s)", containerName)
glog.V(2).Infof("Api - Container(%s)", containerName)
// Get the query request.
query, err := getContainerInfoRequest(r.Body)
@ -137,7 +137,7 @@ func handleRequest(m manager.Manager, w http.ResponseWriter, r *http.Request) er
// The container name is the path after the requestType.
containerName := path.Join("/", strings.Join(requestArgs, "/"))
log.Printf("Api - Subcontainers(%s)", containerName)
glog.V(2).Infof("Api - Subcontainers(%s)", containerName)
// Get the query request.
query, err := getContainerInfoRequest(r.Body)
@ -161,7 +161,7 @@ func handleRequest(m manager.Manager, w http.ResponseWriter, r *http.Request) er
return fmt.Errorf("unknown API request type %q", requestType)
}
log.Printf("Request took %s", time.Since(start))
glog.V(2).Infof("Request took %s", time.Since(start))
return nil
}

BIN
cadvisor

Binary file not shown.

View File

@ -17,9 +17,9 @@ package main
import (
"flag"
"fmt"
"log"
"net/http"
"github.com/golang/glog"
"github.com/google/cadvisor/api"
"github.com/google/cadvisor/container/docker"
"github.com/google/cadvisor/container/raw"
@ -38,22 +38,22 @@ func main() {
storageDriver, err := NewStorageDriver(*argDbDriver)
if err != nil {
log.Fatalf("Failed to connect to database: %s", err)
glog.Fatalf("Failed to connect to database: %s", err)
}
containerManager, err := manager.New(storageDriver)
if err != nil {
log.Fatalf("Failed to create a Container Manager: %s", err)
glog.Fatalf("Failed to create a Container Manager: %s", err)
}
// Register Docker.
if err := docker.Register(containerManager); err != nil {
log.Printf("Docker registration failed: %v.", err)
glog.Errorf("Docker registration failed: %v.", err)
}
// Register the raw driver.
if err := raw.Register(containerManager); err != nil {
log.Fatalf("raw registration failed: %v.", err)
glog.Fatalf("raw registration failed: %v.", err)
}
// Handler for static content.
@ -66,7 +66,7 @@ func main() {
// Register API handler.
if err := api.RegisterHandlers(containerManager); err != nil {
log.Fatalf("failed to register API handlers: %s", err)
glog.Fatalf("failed to register API handlers: %s", err)
}
// Redirect / to containers page.
@ -80,14 +80,16 @@ func main() {
}
})
defer glog.Flush()
go func() {
log.Fatal(containerManager.Start())
glog.Fatal(containerManager.Start())
}()
log.Printf("Starting cAdvisor version: %q", info.VERSION)
log.Print("About to serve on port ", *argPort)
glog.Infof("Starting cAdvisor version: %q", info.VERSION)
glog.Infof("About to serve on port ", *argPort)
addr := fmt.Sprintf(":%v", *argPort)
log.Fatal(http.ListenAndServe(addr, nil))
glog.Fatal(http.ListenAndServe(addr, nil))
}

View File

@ -17,7 +17,6 @@ package docker
import (
"flag"
"fmt"
"log"
"path"
"regexp"
"strconv"
@ -25,6 +24,7 @@ import (
"github.com/docker/libcontainer/cgroups/systemd"
"github.com/fsouza/go-dockerclient"
"github.com/golang/glog"
"github.com/google/cadvisor/container"
"github.com/google/cadvisor/info"
)
@ -134,9 +134,9 @@ func Register(factory info.MachineInfoFactory) error {
client: client,
}
if f.useSystemd {
log.Printf("System is using systemd")
glog.Infof("System is using systemd")
}
log.Printf("Registering Docker factory")
glog.Infof("Registering Docker factory")
container.RegisterContainerHandlerFactory(f)
return nil
}

View File

@ -18,7 +18,6 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"math"
"os"
"path"
@ -28,6 +27,7 @@ import (
"github.com/docker/libcontainer/cgroups"
"github.com/docker/libcontainer/cgroups/fs"
"github.com/fsouza/go-dockerclient"
"github.com/golang/glog"
"github.com/google/cadvisor/container"
containerLibcontainer "github.com/google/cadvisor/container/libcontainer"
"github.com/google/cadvisor/info"
@ -197,7 +197,7 @@ func (self *dockerContainerHandler) GetStats() (stats *info.ContainerStats, err
config, err := self.readLibcontainerConfig()
if err != nil {
if err == fileNotFound {
log.Printf("Libcontainer config not found for container %q", self.name)
glog.Errorf("Libcontainer config not found for container %q", self.name)
return &info.ContainerStats{}, nil
}
return
@ -205,7 +205,7 @@ func (self *dockerContainerHandler) GetStats() (stats *info.ContainerStats, err
state, err := self.readLibcontainerState()
if err != nil {
if err == fileNotFound {
log.Printf("Libcontainer state not found for container %q", self.name)
glog.Errorf("Libcontainer state not found for container %q", self.name)
return &info.ContainerStats{}, nil
}
return

View File

@ -16,8 +16,9 @@ package container
import (
"fmt"
"log"
"sync"
"github.com/golang/glog"
)
type ContainerHandlerFactory interface {
@ -55,7 +56,7 @@ func NewContainerHandler(name string) (ContainerHandler, error) {
// Create the ContainerHandler with the first factory that supports it.
for _, factory := range factories {
if factory.CanHandle(name) {
log.Printf("Using factory %q for container %q", factory.String(), name)
glog.V(1).Infof("Using factory %q for container %q", factory.String(), name)
return factory.NewContainerHandler(name)
}
}

View File

@ -16,9 +16,9 @@ package raw
import (
"fmt"
"log"
"github.com/docker/libcontainer/cgroups"
"github.com/golang/glog"
"github.com/google/cadvisor/container"
"github.com/google/cadvisor/info"
)
@ -75,7 +75,7 @@ func Register(machineInfoFactory info.MachineInfoFactory) error {
return fmt.Errorf("failed to find supported cgroup mounts for the raw factory")
}
log.Printf("Registering Raw factory")
glog.Infof("Registering Raw factory")
factory := &rawFactory{
machineInfoFactory: machineInfoFactory,
cgroupSubsystems: &cgroupSubsystems{

View File

@ -17,13 +17,13 @@ package raw
import (
"fmt"
"io/ioutil"
"log"
"path"
"strconv"
"strings"
"github.com/docker/libcontainer/cgroups"
"github.com/docker/libcontainer/cgroups/fs"
"github.com/golang/glog"
"github.com/google/cadvisor/container"
"github.com/google/cadvisor/container/libcontainer"
"github.com/google/cadvisor/info"
@ -67,7 +67,7 @@ func readString(dirpath string, file string) string {
// Read
out, err := ioutil.ReadFile(cgroupFile)
if err != nil {
log.Printf("raw driver: Failed to read %q: %s", cgroupFile, err)
glog.Errorf("raw driver: Failed to read %q: %s", cgroupFile, err)
return ""
}
return string(out)
@ -81,7 +81,7 @@ func readInt64(dirpath string, file string) uint64 {
val, err := strconv.ParseUint(strings.TrimSpace(out), 10, 64)
if err != nil {
log.Printf("raw driver: Failed to parse in %q from file %q: %s", out, path.Join(dirpath, file), err)
glog.Errorf("raw driver: Failed to parse int %q from file %q: %s", out, path.Join(dirpath, file), err)
return 0
}

View File

@ -18,10 +18,10 @@ package manager
import (
"fmt"
"log"
"sync"
"time"
"github.com/golang/glog"
"github.com/google/cadvisor/container"
"github.com/google/cadvisor/info"
"github.com/google/cadvisor/storage"
@ -51,7 +51,7 @@ type containerData struct {
func (c *containerData) Start() error {
// Force the first update.
c.housekeepingTick()
log.Printf("Start housekeeping for container %q\n", c.info.Name)
glog.Infof("Start housekeeping for container %q\n", c.info.Name)
go c.housekeeping()
return nil
@ -119,7 +119,7 @@ func (c *containerData) housekeeping() {
// Log if housekeeping took longer than 120ms.
duration := time.Since(start)
if duration >= 120*time.Millisecond {
log.Printf("Housekeeping(%s) took %s", c.info.Name, duration)
glog.V(2).Infof("Housekeeping(%s) took %s", c.info.Name, duration)
}
}
}
@ -128,7 +128,7 @@ func (c *containerData) housekeeping() {
func (c *containerData) housekeepingTick() {
err := c.updateStats()
if err != nil {
log.Printf("Failed to update stats for container \"%s\": %s", c.info.Name, err)
glog.Infof("Failed to update stats for container \"%s\": %s", c.info.Name, err)
}
}

View File

@ -16,12 +16,12 @@ package manager
import (
"fmt"
"log"
"path"
"strings"
"sync"
"time"
"github.com/golang/glog"
"github.com/google/cadvisor/container"
"github.com/google/cadvisor/info"
"github.com/google/cadvisor/storage"
@ -56,14 +56,14 @@ func New(driver storage.StorageDriver) (Manager, error) {
return nil, err
}
newManager.machineInfo = *machineInfo
log.Printf("Machine: %+v", newManager.machineInfo)
glog.Infof("Machine: %+v", newManager.machineInfo)
versionInfo, err := getVersionInfo()
if err != nil {
return nil, err
}
newManager.versionInfo = *versionInfo
log.Printf("Version: %+v", newManager.versionInfo)
glog.Infof("Version: %+v", newManager.versionInfo)
newManager.storageDriver = driver
return newManager, nil
@ -84,12 +84,12 @@ func (m *manager) Start() error {
if err != nil {
return err
}
log.Printf("Starting recovery of all containers")
glog.Infof("Starting recovery of all containers")
err = m.detectContainers()
if err != nil {
return err
}
log.Printf("Recovery completed")
glog.Infof("Recovery completed")
// Look for new containers in the main housekeeping thread.
for t := range time.Tick(time.Second) {
@ -98,13 +98,13 @@ func (m *manager) Start() error {
// Check for new containers.
err = m.detectContainers()
if err != nil {
log.Printf("Failed to detect containers: %s", err)
glog.Errorf("Failed to detect containers: %s", err)
}
// Log if housekeeping took more than 100ms.
duration := time.Since(start)
if duration >= 100*time.Millisecond {
log.Printf("Global Housekeeping(%d) took %s", t.Unix(), duration)
glog.V(1).Infof("Global Housekeeping(%d) took %s", t.Unix(), duration)
}
}
return nil
@ -242,7 +242,7 @@ func (m *manager) createContainer(containerName string) (*containerData, error)
m.containers[alias] = cont
}
}()
log.Printf("Added container: %q (aliases: %s)", containerName, cont.info.Aliases)
glog.Infof("Added container: %q (aliases: %s)", containerName, cont.info.Aliases)
// Start the container's housekeeping.
cont.Start()
@ -269,7 +269,7 @@ func (m *manager) destroyContainer(containerName string) error {
for _, alias := range cont.info.Aliases {
delete(m.containers, alias)
}
log.Printf("Destroyed container: %s (aliases: %s)", containerName, cont.info.Aliases)
glog.Infof("Destroyed container: %s (aliases: %s)", containerName, cont.info.Aliases)
return nil
}
@ -325,7 +325,7 @@ func (m *manager) detectContainers() error {
for _, cont := range added {
_, err = m.createContainer(cont.Name)
if err != nil {
log.Printf("failed to create existing container: %s: %s", cont.Name, err)
glog.Errorf("failed to create existing container: %s: %s", cont.Name, err)
}
}
@ -333,7 +333,7 @@ func (m *manager) detectContainers() error {
for _, cont := range removed {
err = m.destroyContainer(cont.Name)
if err != nil {
log.Printf("failed to destroy existing container: %s: %s", cont.Name, err)
glog.Errorf("failed to destroy existing container: %s: %s", cont.Name, err)
}
}

View File

@ -18,7 +18,6 @@ package pages
import (
"fmt"
"html/template"
"log"
"math"
"net/http"
"net/url"
@ -27,6 +26,7 @@ import (
"strings"
"time"
"github.com/golang/glog"
"github.com/google/cadvisor/info"
"github.com/google/cadvisor/manager"
)
@ -66,7 +66,7 @@ func init() {
pageTemplate = template.New("containersTemplate").Funcs(funcMap)
_, err := pageTemplate.Parse(containersHtmlTemplate)
if err != nil {
log.Fatalf("Failed to parse template: %s", err)
glog.Fatalf("Failed to parse template: %s", err)
}
}
@ -263,9 +263,9 @@ func ServerContainersPage(m manager.Manager, w http.ResponseWriter, u *url.URL)
}
err = pageTemplate.Execute(w, data)
if err != nil {
log.Printf("Failed to apply template: %s", err)
glog.Errorf("Failed to apply template: %s", err)
}
log.Printf("Request took %s", time.Since(start))
glog.V(1).Infof("Request took %s", time.Since(start))
return nil
}

View File

@ -100,7 +100,7 @@ func (self simpleSchedulerLoadReader) Load(container string) ([]int, error) {
func (self simpleSchedulerLoadReader) AllContainers() ([]string, error) {
ret := make([]string, 0, len(self))
for c, _ := range self {
for c := range self {
ret = append(ret, c)
}
return ret, nil