Git Product home page Git Product logo

log's People

Contributors

alexcesaro avatar marcusva avatar shazow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

log's Issues

Issue/Feature request: setLoggingLevel

I am working on a command line application and am using stdlog. Currently, the only way I can see to set the log level is to use GetFromFlags(). Although this works, by default the logging level is set to "Info". What I would like to be able to do is define my own flag and pass the value of that flag to a function such as setLoggingLevel. This would allow me to have the set the default value for the flag as "none" (or I suppose any other non-info level) and have the logger function otherwise the same.

proposal: Add log.NullLogger

I'd like to propose adding a NullLogger instance to the log package which implements a no-op version of log.Logger.

This is obviously a minor improvement, but I run into it often enough that I figure it's worth asking. :) I'd be happy to do a PR for this if you'd like.

Use case

In all of my subpackages, I like to use a package-global log.Logger instance, it typically looks like this:

package foo

import (
    "io/ioutil"

    "github.com/alexcesaro/log"
    "github.com/alexcesaro/log/golog"
)

var logger log.Logger

func SetLogger(l log.Logger) {
    logger = l
}

func init() {
    // Set a default null logger
    SetLogger(golog.New(ioutil.Discard, log.None))
}

Ideally, I'd like for this boilerplate file to be more like:

package foo

import "github.com/alexcesaro/log"

var logger log.Logger = log.NullLogger

func SetLogger(l log.Logger) {
    logger = l
}

Please make a release

Making a proper release helps packaging your software by 3rd-party vendors like linux distros or BSDs.

It's super easy on github, please tag a release.

Many thanks,
Alex

Data race condition warning

Hi there, this may be out of scope for your module but you may like to know that there is a race condition warning for when this module is used with goroutines.

Exhibited by this test case (in go v1.2.2):

package main

import (
    "os"

    "github.com/alexcesaro/log"
    "github.com/alexcesaro/log/golog"
)

var logger *golog.Logger

func main() {
    logger = golog.New(os.Stderr, log.Debug)
    done := make(chan struct{})

    logger.Info("Test.")

    go func() {
        logger.Info("Test.")
        done <- struct{}{}
    }()

    logger.Info("Test.")

    <-done
}

This yields warnings like this...

$ go run -race main.go
2014-05-20 15:43:44.294 INFO Test.
2014-05-20 15:43:44.298 INFO Test.
==================
WARNING: DATA RACE
Write by goroutine 3:
  runtime.copy()
      /usr/local/Cellar/go/1.2.2/libexec/src/pkg/runtime/slice.c:120 +0x0
  bytes.(*Buffer).Write()
      /usr/local/Cellar/go/1.2.2/libexec/src/pkg/bytes/buffer.go:128 +0x11d
  github.com/alexcesaro/log/golog.addTimestamp()
      /Users/shazow/local/go/src/github.com/alexcesaro/log/golog/golog.go:252 +0x394
  github.com/alexcesaro/log/golog.func·001()
      /Users/shazow/local/go/src/github.com/alexcesaro/log/golog/golog.go:225 +0x34
  github.com/alexcesaro/log/golog.(*Logger).output()
      /Users/shazow/local/go/src/github.com/alexcesaro/log/golog/golog.go:215 +0xec
  github.com/alexcesaro/log/golog.(*Logger).Info()
      /Users/shazow/local/go/src/github.com/alexcesaro/log/golog/golog.go:129 +0x57
  main.func·001()
      /Users/shazow/tmp/go/main.go:19 +0xf1

Previous read by main goroutine:
  syscall.raceReadRange()
      /usr/local/Cellar/go/1.2.2/libexec/src/pkg/syscall/race.go:25 +0x3f
  syscall.Write()
      /usr/local/Cellar/go/1.2.2/libexec/src/pkg/syscall/syscall_unix.go:153 +0xb9
  os.(*File).write()
      /usr/local/Cellar/go/1.2.2/libexec/src/pkg/os/file_unix.go:194 +0x9d
  os.(*File).Write()
      /usr/local/Cellar/go/1.2.2/libexec/src/pkg/os/file.go:139 +0xbb
  github.com/alexcesaro/log/golog.func·002()
      /Users/shazow/local/go/src/github.com/alexcesaro/log/golog/golog.go:285 +0x58
  github.com/alexcesaro/log/golog.(*Logger).output()
      /Users/shazow/local/go/src/github.com/alexcesaro/log/golog/golog.go:220 +0x236
  github.com/alexcesaro/log/golog.(*Logger).Info()
      /Users/shazow/local/go/src/github.com/alexcesaro/log/golog/golog.go:129 +0x57
  main.main()
      /Users/shazow/tmp/go/main.go:23 +0x3a5

Goroutine 3 (running) created at:
  main.main()
      /Users/shazow/tmp/go/main.go:21 +0x2dc

For comparison, using the stdlib "log" module works fine:

package main

import (
    "log"
    "os"
)

var logger *log.Logger

func main() {
    logger = log.New(os.Stderr, "", log.LstdFlags)
    done := make(chan struct{})

    logger.Print("Test.")

    go func() {
        logger.Print("Test.")
        done <- struct{}{}
    }()

    logger.Print("Test.")

    <-done
}
$ go run -race main.go
2014/05/20 15:47:27 Test.
2014/05/20 15:47:27 Test.
2014/05/20 15:47:27 Test.

By the way, thank you for building this otherwise-excellent logging module. :) It's exactly what I needed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.