File: example.go

package info (click to toggle)
golang-github-client9-reopen 1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 112 kB
  • sloc: sh: 41; makefile: 39
file content (40 lines) | stat: -rw-r--r-- 769 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main

/* Simple logrotate logger
 */
import (
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"

	"github.com/client9/reopen"
)

func main() {
	f, err := reopen.NewFileWriter("/tmp/example.log")
	if err != nil {
		log.Fatalf("Unable to set output log: %s", err)
	}
	log.SetOutput(f)

	// channel is number of signals needed to catch  (more or less)
	// we only are working with one here, SIGUP
	sighup := make(chan os.Signal, 1)
	signal.Notify(sighup, syscall.SIGHUP)
	go func() {
		for {
			<-sighup
			fmt.Printf("Got a sighup\n")
			f.Reopen()
		}
	}()

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		log.Printf("%s", r.URL.Path)
		fmt.Fprintf(w, "%s\n", r.URL.Path)
	})
	log.Fatal(http.ListenAndServe("127.0.0.1:8123", nil))
}