Output file in log

675 views
Skip to first unread message

Archos

unread,
Nov 21, 2010, 12:05:16 PM11/21/10
to golang-nuts
The field "out" in type "log.Logger"[1] should be public so the file
could be closed when is used another one than "os.Stderr".

If not, why to have "log.SetOutput()" to set another file but unable
to close it.


[1]: http://golang.org/src/pkg/log/log.go#L38

chris dollin

unread,
Nov 21, 2010, 12:13:13 PM11/21/10
to Archos, golang-nuts
On 21 November 2010 17:05, Archos <raul...@sent.com> wrote:
> The field "out" in type "log.Logger"[1] should be public so the file
> could be closed when is used another one than "os.Stderr".
>
> If not, why to have "log.SetOutput()" to set another file but unable
> to close it.

Um, exposing the field `out` doesn't help (much) because it's
an io.Writer with no close method anyway. You could, I suppose,
do the type-test dance.

If you're going to tinker with the log output stream anyway,
you could keep track of the file (if it's a file) that you've set it
to and close it directly.

Chris

--
Chris "allusive" Dollin

Archos

unread,
Nov 21, 2010, 12:17:05 PM11/21/10
to golang-nuts


On Nov 21, 5:13 pm, chris dollin <ehog.he...@googlemail.com> wrote:
> On 21 November 2010 17:05, Archos <raul....@sent.com> wrote:
>
> > The field "out" in type "log.Logger"[1] should be public so the file
> > could be closed when is used another one than "os.Stderr".
>
> > If not, why to have "log.SetOutput()" to set another file but unable
> > to close it.
>
> Um, exposing the field `out` doesn't help (much) because it's
> an io.Writer with no close method anyway. You could, I suppose,
> do the type-test dance.
Right. For it, instead of "out io.Writer" should be "out *os.File".

Anthony Martin

unread,
Nov 21, 2010, 12:20:08 PM11/21/10
to Archos, golang-nuts
Archos <raul...@sent.com> once said:
> The field "out" in type "log.Logger"[1] should be public so the file
> could be closed when is used another one than "os.Stderr".
>
> If not, why to have "log.SetOutput()" to set another file but unable
> to close it.

One of the nice things about the log package is
that it simply wraps an io.Writer. The client
is responsible for setting it up beforehand and
possibly closing it when no longer needed.

Also, remember that the io.Writer interface has
only one method, namely Write. It's possible to
have a type that satisfies this interface yet
has no notion of "closing".

Anthony

chris dollin

unread,
Nov 21, 2010, 12:24:16 PM11/21/10
to Archos, golang-nuts
On 21 November 2010 17:17, Archos <raul...@sent.com> wrote:

> Right. For it, instead of "out io.Writer" should be "out *os.File".

No, it shouldn't. There's no reason to constrain the logging output
to go directly to a file.

Ostsol

unread,
Nov 21, 2010, 2:48:47 PM11/21/10
to golang-nuts
Note that the standard logger is meant to be generic, and not just for
files. Perhaps a bytes.Buffer, or stdout/stderr -- which I'm not sure
should be closed --, or some custom output object. In any case, it is
trivial to just append your own functionality to the standard logger.

type MyLogger struct {
*log.Logger
out io.WriteCloser
}

func NewMyLogger(out io.WriteCloser, prefix string, flag int) *Logger
{
return &MyLogger{log.New(out, prefix, flag), out}
}

func (self *MyLogger) Close() os.Error {
self.Println("Closing logger")
return self.out.Close()
}

-Daniel

Johann Höchtl

unread,
Nov 22, 2010, 3:55:22 AM11/22/10
to golang-nuts


On Nov 21, 8:48 pm, Ostsol <ost...@gmail.com> wrote:
> Note that the standard logger is meant to be generic, and not just for
> files.  Perhaps a bytes.Buffer, or stdout/stderr -- which I'm not sure
> should be closed --, or some custom output object.  In any case, it is
> trivial to just append your own functionality to the standard logger.
>
I used to write a generic Logger many years back. We settled on the
design, that a logger should not use buffering as when your app goes
awry you certainly want to see where it fails. Bytes of a logger
should be flushed immediately.
Reply all
Reply to author
Forward
0 new messages