FAQ
Hey,

I try to write a little logging package for my program.
It should create a file if it doesn't exist and append a given input
(string) into this file.
Most functions I tried so far overwrite the textfile :(

func WriteEntry (logfiletype, entry string){
datestamp := time.Now()
datestampstring := datestamp.String()
datestampstring = datestampstring[0:10]
datestampstring = logfiletype + "-" + datestampstring
os.Chdir("../logs")
logfile,_ := os.Create(datestampstring)
logfile.WriteString(entry)
logfile.Close()
}

Thanks
Frank

--

Search Discussions

  • Jan Mercl at Oct 1, 2012 at 3:04 pm

    On Mon, Oct 1, 2012 at 4:57 PM, Frank Blechschmidt wrote:
    Hey,

    I try to write a little logging package for my program.
    It should create a file if it doesn't exist and append a given input
    (string) into this file.
    Most functions I tried so far overwrite the textfile :(
    func WriteEntry (logfiletype, entry string){
    datestamp := time.Now()
    datestampstring := datestamp.String()
    datestampstring = datestampstring[0:10]
    datestampstring = logfiletype + "-" + datestampstring
    os.Chdir("../logs")
    logfile,_ := os.Create(datestampstring)
    logfile.WriteString(entry)
    logfile.Close()
    }
    Instead of os.Create better is probably os.OpenFile(name,
    os.O_WRONLY|os.O_APPEND|os.O_CREATE, perm) in this case.

    cf: http://golang.org/pkg/os/#OpenFile

    -j

    --
  • Frank Blechschmidt at Oct 1, 2012 at 3:08 pm
    Works fine! Many thanks :D
    On Monday, October 1, 2012 5:04:45 PM UTC+2, Jan Mercl wrote:
    On Mon, Oct 1, 2012 at 4:57 PM, Frank Blechschmidt wrote:
    Hey,

    I try to write a little logging package for my program.
    It should create a file if it doesn't exist and append a given input
    (string) into this file.
    Most functions I tried so far overwrite the textfile :(
    func WriteEntry (logfiletype, entry string){
    datestamp := time.Now()
    datestampstring := datestamp.String()
    datestampstring = datestampstring[0:10]
    datestampstring = logfiletype + "-" + datestampstring
    os.Chdir("../logs")
    logfile,_ := os.Create(datestampstring)
    logfile.WriteString(entry)
    logfile.Close()
    }
    Instead of os.Create better is probably os.OpenFile(name,
    os.O_WRONLY|os.O_APPEND|os.O_CREATE, perm) in this case.

    cf: http://golang.org/pkg/os/#OpenFile

    -j
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 1, '12 at 3:03p
activeOct 1, '12 at 3:08p
posts3
users2
websitegolang.org

2 users in discussion

Frank Blechschmidt: 2 posts Jan Mercl: 1 post

People

Translate

site design / logo © 2022 Grokbase