Пример #1
0
func elabLog(wg *sync.WaitGroup, logFileInfo *agent.LogFileInfo, cnf *agent.Config, statusCnf *agent.StatusConfig) {
	var exist bool
	var toRead bool = false
	var logRecord agent.LogRecord
	var readFrom uint64

	defer logFileInfo.File.Close()

	_log.Printf("elaborating %s \n", logFileInfo)

	log4allClient, err := client.NewClient(cnf.Log4allUrl, cnf.Application, cnf.ApplicationToken)

	if err != nil {
		_log.Fatalf("Error creating client %s\n", err.Error())
		return
	}

	if logRecord, exist = statusCnf.LogMap[strconv.FormatUint(logFileInfo.Inode, 10)]; exist && logRecord.Readed < logFileInfo.Size {
		//partially readed
		_log.Printf("Partially readed readed:%d size:%d\n", logRecord.Readed, logFileInfo.Size)
		readFrom = logRecord.Readed
		toRead = true
	} else {
		if !exist {
			_log.Println("Not readed")
			readFrom = 0
			toRead = true
		}
	}
	if toRead {
		logFile, err := agent.NewLogFile(cnf, logFileInfo.File, readFrom)
		if err != nil {
			_log.Fatalf("Error opening %s:%s", logFileInfo, err.Error())
		} else {
			//parse log file
			logs, err := logFile.Parse()
			if err != nil {
				_log.Printf("Error: %s\n", err.Error())
			}
			//send log to server
			err = log4allClient.AddLogs(logs)
			if err != nil {
				_log.Printf("Error: %s\n", err.Error())
			}
			_log.Printf("logs readed:%d\n", len(logs))
			statusCnf.LogReaded(logFileInfo.Inode, logFileInfo.Name, logFile.Readed)
		}
	}
	wg.Done()
}
Пример #2
0
func addCommandExec() {
	cl, err := client.NewClient(*addCmd_Url, *addCmd_Application, *addCmd_ApplicationToken)
	if err != nil {
		printError("Error: %s", err.Error())
		os.Exit(1)
	}
	stat, _ := os.Stdin.Stat()
	stack := ""
	if (stat.Mode() & os.ModeCharDevice) == 0 {
		stackBytes, err := ioutil.ReadAll(os.Stdin)
		if err == nil {
			stack = string(stackBytes)
		}
	}
	lg, err := commons.NewLog(time.Now().UnixNano()/1000000, *addCmd_Level, *addCmd_Message, stack)
	if err != nil {
		printError("Error: %s", err.Error())
		os.Exit(1)
	}
	cl.AddLog(lg)
}
Пример #3
0
func searchCommandExec() {
	if !strings.EqualFold(*searchCmd_Format, "json") && !strings.EqualFold(*searchCmd_Format, "jsonIndented") && !strings.EqualFold(*searchCmd_Format, "plain") {
		printError("Error: format %s not supported", *searchCmd_Format)
		os.Exit(1)
	}

	cl, err := client.NewClient(*searchCmd_Url, *addCmd_Application, *addCmd_ApplicationToken)
	if err != nil {
		printError("Error: %s", err.Error())
		os.Exit(1)
	}
	srcQuery := new(commons.LogSearchParam)
	srcQuery.Applications = *searchCmd_Applications
	dtSince, err := time.Parse("2006-01-02 15:04:05", *searchCmd_Since)
	if err != nil {
		printError("Error: %s", err.Error())
		os.Exit(1)
	}
	srcQuery.DtSince = dtSince.UnixNano() / 1000000
	dtTo, err := time.Parse("2006-01-02 15:04:05", *searchCmd_To)
	if err != nil {
		printError("Error: %s", err.Error())
		os.Exit(1)
	}
	srcQuery.DtTo = dtTo.UnixNano() / 1000000
	srcQuery.Levels = *searchCmd_Levels
	srcQuery.Query = *searchCmd_Query
	srcQuery.MaxResult = *searchCmd_MaxResult
	srcQuery.Page = *searchCmd_Page
	srcQuery.SortField = *searchCmd_SortField
	srcQuery.SortAscending = !(*searchCmd_Reverse)

	logs, err := cl.Search(srcQuery)
	if err != nil {
		printError("Error: %s", err.Error())
		os.Exit(1)
	}
	var result []byte

	if strings.EqualFold(*searchCmd_Format, "json") {
		result, err = json.Marshal(logs)
	}
	if strings.EqualFold(*searchCmd_Format, "jsonIndented") {
		result, err = json.MarshalIndent(logs, "", "\t")
	}
	if strings.EqualFold(*searchCmd_Format, "plain") {
		var plainLogs string
		for _, log := range logs {
			plainLogs = plainLogs + "[" + log.Application + "]: " + log.Date.String() + " " + log.Level + " " + log.Message + "\n"
			if len(log.Stack) > 0 {
				plainLogs = plainLogs + log.Stack
			}
		}
		result = []byte(plainLogs)
	}
	if err != nil {
		printError("Error: %s", err.Error())
		os.Exit(1)
	}
	fmt.Printf("%s\n", string(result))
}