Skip to content

Instantly share code, notes, and snippets.

@nicewook
Last active June 14, 2019 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicewook/8ab51cc920b242f3fed6d560dd790afa to your computer and use it in GitHub Desktop.
Save nicewook/8ab51cc920b242f3fed6d560dd790afa to your computer and use it in GitHub Desktop.
package main
import (
// "fmt"
"log"
"os"
"runtime"
"strconv"
"sync"
"time"
)
var fileLogger *log.Logger
func main() {
runtime.GOMAXPROCS(1)
log.Println("Concurrently logging to File...")
var wait sync.WaitGroup
rNum := 100
wait.Add(rNum)
fn := "concurrent_log.log"
f, err := os.OpenFile(fn, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
defer f.Close()
if err != nil {
log.Fatal(err)
}
fileLogger = log.New(f, "", 0)
for i := 0; i < rNum; i++ {
go func(i int, f *os.File) {
defer wait.Done()
for j := 0; j < 1000; j++ {
tLog := time.Now().Format("060102_150405")
tLog = strconv.Itoa(i) + ": " + tLog //+ "\n"
log.Println(tLog)
fileLogger.Output(1, tLog)
// f.WriteString(tLog)
}
}(i, f)
}
wait.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment