FAQ
Is there a nice way to do the same, but have different logging parameters
for each output? For example, I'd like the data logged to file to have
date and timestamps, while the version written to stdout to be bare, just
the messages themselves? I suspect there is no clever way to do that,
short of writing a module which specifically does this itself...
On Monday, May 30, 2011 11:45:39 PM UTC-4, Jessta wrote:

On Tue, May 31, 2011 at 1:35 PM, dlin <dli...@gmail.com <javascript:>>
wrote:
Thanks,

But, I still don't know how to separate to behavior of normal message
and fatal message.

log.Println just send to logf.
log.Fatalln send to both console and logf.
You'll have to create another log.Logger
http://golang.org/pkg/log/#Logger.New
here is my code.

logf, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE,
0640)
if err != nil {
log.Fatalln(err)
}
log.SetOutput(logf)
mylogger := log.New(io.MultiWriter(logf,os.Stdout)
log.Println("goes to logf")
mylogger.Fatalln("goes to stdout and logf")

--
=====================
http://jessta.id.au

Search Discussions

  • Kyle Lemons at Sep 4, 2012 at 6:56 pm
    If you have specialized logging requirements along those lines, you can
    always try out log4go <http://code.google.com/p/log4go/> (full disclosure:
    mine).
    On Mon, Sep 3, 2012 at 10:01 AM, wrote:

    Is there a nice way to do the same, but have different logging parameters
    for each output? For example, I'd like the data logged to file to have
    date and timestamps, while the version written to stdout to be bare, just
    the messages themselves? I suspect there is no clever way to do that,
    short of writing a module which specifically does this itself...
    On Monday, May 30, 2011 11:45:39 PM UTC-4, Jessta wrote:
    On Tue, May 31, 2011 at 1:35 PM, dlin wrote:
    Thanks,

    But, I still don't know how to separate to behavior of normal message
    and fatal message.

    log.Println just send to logf.
    log.Fatalln send to both console and logf.
    You'll have to create another log.Logger
    http://golang.org/pkg/log/#**Logger.New<http://golang.org/pkg/log/#Logger.New>
    here is my code.

    logf, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE,
    0640)
    if err != nil {
    log.Fatalln(err)
    }
    log.SetOutput(logf)
    mylogger := log.New(io.MultiWriter(logf,**os.Stdout)
    log.Println("goes to logf")
    mylogger.Fatalln("goes to stdout and logf")

    --
    =====================
    http://jessta.id.au

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 3, '12 at 10:43p
activeSep 4, '12 at 6:56p
posts2
users2
websitegolang.org

2 users in discussion

Mkalisiak: 1 post Kyle Lemons: 1 post

People

Translate

site design / logo © 2021 Grokbase