本文整理匯總了Golang中github.com/hpcloud/tail.TailFile函數的典型用法代碼示例。如果您正苦於以下問題:Golang TailFile函數的具體用法?Golang TailFile怎麽用?Golang TailFile使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了TailFile函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: readLog
func readLog() {
var seek = tail.SeekInfo{Offset: 0, Whence: 2}
tailer, err := tail.TailFile(os.Args[1], tail.Config{
Follow: true,
ReOpen: true,
Location: &seek,
})
if err != nil {
log.Panicln(err)
}
re := regexp.MustCompile(`^(?P<ip>[\d\.]+) - - \[(?P<timestamp>.*)\] "(?P<verb>.*) (?P<query>.*) (?P<proto>.*)" (?P<status>\d+) (?P<bytes>\d+) "(?P<referer>.*)" "(?P<useragent>.*)"`)
for line := range tailer.Lines {
res := re.FindStringSubmatch(line.Text)
ip := res[1]
curtime := time.Now().Local()
verb := res[3]
query := res[4]
proto := res[5]
status, _ := strconv.Atoi(res[6])
bytes, _ := strconv.ParseInt(res[7], 10, 64)
referer := res[8]
useragent := res[9]
logline := RawLogEvent{ip, curtime, verb, query, proto, status, bytes, referer, useragent}
rawlog_output <- logline
logdump_output <- res[0] // Spraynard Kruger
}
}
示例2: main
func main() {
pub, err := nsq.NewProducer(*tHost, nsq.NewConfig())
if err != nil {
log.Fatal(err)
}
t, err := tail.TailFile(*tFile, tail.Config{Follow: true,
//Location: &tail.SeekInfo{Offset: 0, Whence: os.SEEK_END}})
Location: &tail.SeekInfo{Offset: 0, Whence: os.SEEK_CUR}})
if err != nil {
log.Fatal(err)
}
for line := range t.Lines {
if *tTag != "" {
for k, tag := range strings.Split(*tTag, "|") {
var ks = []string{"pv", "click", "other"}
if strings.Contains(line.Text, tag) {
if *tDebug == "1" {
fmt.Println(fmt.Println(line.Text))
}
pub.Publish("log", []byte(ks[k]+"\t"+line.Text))
}
}
}
}
}
示例3: tailFile
func tailFile(ctx context.Context, file string, poll bool, dest *os.File) {
defer wg.Done()
t, err := tail.TailFile(file, tail.Config{
Follow: true,
ReOpen: true,
Poll: poll,
Logger: tail.DiscardingLogger,
})
if err != nil {
log.Fatalf("unable to tail %s: %s", "foo", err)
}
// main loop
for {
select {
// if the channel is done, then exit the loop
case <-ctx.Done():
t.Stop()
t.Cleanup()
return
// get the next log line and echo it out
case line := <-t.Lines:
if line != nil {
fmt.Fprintln(dest, line.Text)
}
}
}
}
示例4: main
func main() {
if len(os.Args) != 3 {
fmt.Println("Usage: canaryeye FILENAME COMMAND")
os.Exit(1)
}
fn, cmd := os.Args[1], os.Args[2]
m := map[string]int{}
begin := time.Now()
go canaryeye.Run(canaryeye.GetConfig(), &m, &begin, cmd)
t, err := tail.TailFile(fn, *canaryeye.GetTailConfig())
canaryeye.HandleError(err)
r, _ := regexp.Compile("^([^ ]+)")
for line := range t.Lines {
matches := r.FindAllStringSubmatch(line.Text, -1)
if len(matches) > 0 {
m[matches[0][1]]++
}
}
}
示例5: tailFile
func tailFile(path string) {
t, err := tail.TailFile(path, tail.Config{
Follow: true,
})
_ = t
if err != nil {
return
}
for {
select {
case line := <-t.Lines:
logstream <- types.Message{
FileName: t.Filename,
Text: line.Text,
}
case fn := <-RemoveTail:
if path == fn {
log.Info("remove file %s\n", fn)
return
} else {
RemoveTail <- fn
}
}
}
}
示例6: follow_and_analyze
func follow_and_analyze(filename string, regexes []string, c chan incidentstore.Incident) {
t, err := tail.TailFile(filename,
tail.Config{
Follow: true, // actually follow the logs
ReOpen: true, // allow logs to be rotated
Location: &tail.SeekInfo{Offset: 0, Whence: 2}}) // seek to end of file
if err != nil {
log.Fatal(err)
}
var compiledRegexes []regexp.Regexp
for _, regex := range regexes {
compiledRegex := regexp.MustCompile(regex)
compiledRegexes = append(compiledRegexes, *compiledRegex)
}
for line := range t.Lines {
// match each line against all regexes
for _, regex := range compiledRegexes {
result := regex.FindStringSubmatch(line.Text)
if result != nil {
ip := net.ParseIP(result[1])
if ip != nil {
c <- incidentstore.Incident{Filename: filename, Ip: ip, Time: time.Now(), Line: line.Text}
// break here, this line matched on regex
break
}
}
}
}
}
示例7: Logs
func (s *apiServer) Logs(r *types.LogsRequest, stream types.API_LogsServer) error {
state, err := s.getState(r.Id)
if err != nil {
return err
}
// if the job has completed or failed then we cannot follow
follow := r.Follow
if string(state) == string(jobFailed) || string(state) == string(jobCompleted) {
follow = false
}
stdout := filepath.Join(s.StateDir, string(jobIDByte(r.Id)), "stdout")
t, err := tail.TailFile(stdout, tail.Config{
Follow: follow,
MustExist: true,
})
if err != nil {
return fmt.Errorf("Tail file %s failed: %v", stdout, err)
}
for line := range t.Lines {
if err := stream.Send(&types.Log{Log: line.Text}); err != nil {
return fmt.Errorf("Sending log to stream failed: %v", err)
}
}
return nil
}
示例8: main
func main() {
ConfigBytes, err := ioutil.ReadFile(*ConfigFile)
if err != nil {
log.Fatalf("Error reading config file %s\n", err)
}
err = json.Unmarshal(ConfigBytes, &Config)
if err != nil {
log.Fatalf("Error parsing config file %s\n", err)
}
for _, endpointConfig := range Config.Endpoints {
log.Printf("Starting up %s handler", endpointConfig.Driver)
endpointConfig.e = endpointDrivers[endpointConfig.Driver](endpointConfig.Options)
endpointConfig.e.HandleMessage(Message)
endpointConfig.e.Run()
}
for name, logConfig := range Config.Logs {
go func(name string, logConfig *Log) {
logfile, err := tail.TailFile(logConfig.File, tail.Config{Location: &tail.SeekInfo{Whence: os.SEEK_END}, Follow: true, ReOpen: true})
if err != nil {
log.Printf("Error tailing file: %s", err)
}
var filter *regexp.Regexp
if logConfig.Regex != "" {
var err error
filter, err = regexp.Compile(logConfig.Regex)
if err != nil {
log.Printf("Error compiling regex: %s", err)
}
}
for line := range logfile.Lines {
if line.Err != nil {
log.Printf("Error tailing file: %s", line.Err)
}
if filter == nil || filter.MatchString(line.Text) {
logConfig.lines = append(logConfig.lines, line)
if len(logConfig.lines) > logConfig.Keep {
logConfig.lines = logConfig.lines[len(logConfig.lines)-logConfig.Keep:]
}
/*if logConfig.Live {
for _, channel := range logConfig.Channels {
c.Privmsg(channel, line.Text)
}
}*/
}
}
}(name, logConfig)
}
for _, monitorConfig := range Config.Monitors {
monitorConfig.monitor = monitorDrivers[monitorConfig.Driver](monitorConfig.Options)
monitorConfig.track = newMonitorTrack()
monitorConfig.track.Start(monitorConfig.monitor)
}
quit := make(chan bool)
<-quit
}
示例9: open
func (recv ChromebusRecordTailReceiver) open() {
var err error
recv.t, err = tail.TailFile(Events, tail.Config{Follow: true})
if err != nil {
log.Fatal(err)
}
recv.opened = true
}
示例10: WatchLog
// Creates a Watcher around a watched file.
func WatchLog(watched string) Watcher {
tail, err := tail.TailFile(watched, tail.Config{Follow: true, ReOpen: true})
if err != nil {
log.WithError(err).Fatal()
panic(err)
}
return Watcher{Tail: tail}
}
示例11: createTailReader
func (i *Tail) createTailReader(config tail.Config) {
tail, err := tail.TailFile(i.file, config)
if err != nil {
Critical("tail %s: %v", i.file, err)
}
i.tail = tail
}
示例12: printLogs
// printLogs streams the content of a file on the standard output.
// When it stops working, it sends a message in the stopNotifier channel.
func (d *Daemon) printLogs(filename string, stopNotifier chan<- string) {
t, _ := tail.TailFile(filename, tail.Config{Follow: true})
for line := range t.Lines {
fmt.Println(line.Text)
}
stopNotifier <- fmt.Sprintf("could not stream logs from `%s`", filename)
}
示例13: OpenLogfile
// OpenLogfile opens a logfile and passes back a *tail.Tail pointer.
func OpenLogfile(logfile string) *tail.Tail {
t, err := tail.TailFile(logfile, tail.Config{
Location: &tail.SeekInfo{Whence: os.SEEK_END},
ReOpen: true,
Follow: true})
if err != nil {
Log("There was an error opening the file.", "info")
}
return t
}
示例14: Tailer
func (this *TailsInput) Tailer(f string) error {
var seek int
var offset int64
var err error
pointfile := fmt.Sprintf("%s/%d", this.config.JournalDirectory, hash(f))
if offset, err = readPoint(pointfile); err != nil {
seek = os.SEEK_END
} else {
seek = os.SEEK_SET
}
t, err := tail.TailFile(f, tail.Config{
Poll: true,
ReOpen: true,
Follow: true,
MustExist: false,
Location: &tail.SeekInfo{int64(offset), seek},
})
if err != nil {
return err
}
tick := time.NewTicker(time.Second * time.Duration(3))
count := 0
for {
select {
case <-tick.C:
{
if count > 0 {
offset, err := t.Tell()
if err != nil {
log.Println("Tell return error: ", err)
continue
}
if err = writePoint(pointfile, offset); err != nil {
return err
}
count = 0
}
}
case line := <-t.Lines:
{
pack := <-this.runner.InChan()
pack.MsgBytes = []byte(line.Text)
pack.Msg.Tag = this.common.Tag
pack.Msg.Timestamp = time.Now().Unix()
count++
this.runner.RouterChan() <- pack
}
}
}
err = t.Wait()
return err
}
示例15: tailCConsoleLog
// shows all new lines in cconsole.log
func tailCConsoleLog(inst string) {
folder := getInstanceFolder(inst)
endLocation := tail.SeekInfo{Offset: 0, Whence: os.SEEK_END}
if t, err := tail.TailFile(folder+"/mgr/cconsole.log", tail.Config{Follow: true, Location: &endLocation}); err != nil {
log.Printf("Error while getting content for cconsole.log\n")
log.Printf("ERR: %s.\n", err)
} else {
for line := range t.Lines {
fmt.Println(line.Text)
}
}
}