Example #1
0
func newFileFilter(file string) *log4go.FileLogWriter {
	flw := log4go.NewFileLogWriter(file, false)
	//flw.SetFormat("[%D %T] [%L] (%S) %M")
	flw.SetFormat("[%D %T] [%L] %M")
	flw.SetRotateLines(0)
	flw.SetRotateSize(0)
	flw.SetRotateDaily(false)

	return flw
}
Example #2
0
func initLogger() {
	_logInstance = &GlobalLogger{TAG, make(l4g.Logger, 2)}

	consoleLogWriter := l4g.NewConsoleLogWriter()
	fileLogWriter := l4g.NewFileLogWriter(getLogFilePath(), false)
	fileLogWriter.SetFormat(LOG_FILE_FORMAT)

	_logInstance.AddFilter("stdout", l4g.DEBUG, consoleLogWriter)
	_logInstance.AddFilter("file", l4g.DEBUG, fileLogWriter)
	_logInstance.Info(TAG, "日志模块已启动。")
}
Example #3
0
func setupLogging(loggingLevel, logFile string) {
	level := log.DEBUG
	switch loggingLevel {
	case "fine":
		level = log.FINE
	case "debug":
		level = log.DEBUG
	case "info":
		level = log.INFO
	case "warn":
		level = log.WARNING
	case "error":
		level = log.ERROR
	default:
		log.Error("Unknown log level %s. Defaulting to DEBUG", loggingLevel)
	}

	log.Global = make(map[string]*log.Filter)

	facility, ok := GetSysLogFacility(logFile)
	if ok {
		flw, err := NewSysLogWriter(facility)
		if err != nil {
			fmt.Fprintf(os.Stderr, "NewSysLogWriter: %s\n", err.Error())
			return
		}
		log.AddFilter("syslog", level, flw)
	} else if logFile == "stdout" {
		flw := log.NewConsoleLogWriter()
		log.AddFilter("stdout", level, flw)
	} else {
		logFileDir := filepath.Dir(logFile)
		os.MkdirAll(logFileDir, 0744)

		flw := log.NewFileLogWriter(logFile, false)
		if flw == nil {
			os.Exit(1)
		}
		log.AddFilter("file", level, flw)

		flw.SetFormat("[%D %T] [%L] (%S) %M")
		flw.SetRotate(true)
		flw.SetRotateSize(0)
		flw.SetRotateLines(0)
		flw.SetRotateDaily(true)
	}

	log.Info("Redirectoring logging to %s", logFile)
}
Example #4
0
func configureLog(s *model.LogSettings) {

	l4g.Close()

	if s.EnableConsole {
		level := l4g.DEBUG
		if s.ConsoleLevel == "INFO" {
			level = l4g.INFO
		} else if s.ConsoleLevel == "WARN" {
			level = l4g.WARNING
		} else if s.ConsoleLevel == "ERROR" {
			level = l4g.ERROR
		}

		lw := l4g.NewConsoleLogWriter()
		lw.SetFormat("[%D %T] [%L] %M")
		l4g.AddFilter("stdout", level, lw)
	}

	if s.EnableFile {

		var fileFormat = s.FileFormat

		if fileFormat == "" {
			fileFormat = "[%D %T] [%L] %M"
		}

		level := l4g.DEBUG
		if s.FileLevel == "INFO" {
			level = l4g.INFO
		} else if s.FileLevel == "WARN" {
			level = l4g.WARNING
		} else if s.FileLevel == "ERROR" {
			level = l4g.ERROR
		}

		flw := l4g.NewFileLogWriter(GetLogFileLocation(s.FileLocation), false)
		flw.SetFormat(fileFormat)
		flw.SetRotate(true)
		flw.SetRotateLines(LOG_ROTATE_SIZE)
		l4g.AddFilter("file", level, flw)
	}
}
Example #5
0
func (logManager *LogManager) getLogWithFileName(filePath string) log4go.Logger {
	logger, ok := logManager.loggerMap[filePath]
	if ok {
		return logger
	} else {
		// Create the empty logger
		logger := make(log4go.Logger)
		fileWriter := log4go.NewFileLogWriter(filePath, false)
		fileWriter.SetFormat("[%D %T] [%L] (%S) %M")
		fileWriter.SetRotate(false)
		fileWriter.SetRotateSize(100 * 1024 * 1024)
		fileWriter.SetRotateLines(0)
		fileWriter.SetRotateDaily(false)
		logger.AddFilter("file", log4go.DEBUG, fileWriter)

		logManager.loggerMap[filePath] = logger
		return logger
	}
}
Example #6
0
func LogTo(target string, level_name string) {
	var writer log.LogWriter = nil

	switch target {
	case "stdout":
		writer = log.NewConsoleLogWriter()
	case "none":
		// no logging
	default:
		writer = log.NewFileLogWriter(target, true)
	}

	if writer != nil {
		var level = log.DEBUG

		switch level_name {
		case "FINEST":
			level = log.FINEST
		case "FINE":
			level = log.FINE
		case "DEBUG":
			level = log.DEBUG
		case "TRACE":
			level = log.TRACE
		case "INFO":
			level = log.INFO
		case "WARNING":
			level = log.WARNING
		case "ERROR":
			level = log.ERROR
		case "CRITICAL":
			level = log.CRITICAL
		default:
			level = log.DEBUG
		}

		root.AddFilter("log", level, writer)
	}
}
Example #7
0
func handle(w http.ResponseWriter, r *http.Request) {

	folderPath, _ := osext.ExecutableFolder()

	/*
	   Preparing Logging
	*/
	log := l4g.NewLogger()
	flw := l4g.NewFileLogWriter(folderPath+LogFileName, false)
	clw := l4g.NewConsoleLogWriter()

	flw.SetFormat("[%D %T] [%L] (%S) %M")
	log.AddFilter("file", l4g.FINE, flw)
	log.AddFilter("stdout", l4g.FINE, clw)

	var items []LogRecord

	/*
	   Error cecking, data validation
	*/
	if r.Method != "POST" {
		w.WriteHeader(http.StatusMethodNotAllowed)
		fmt.Fprintf(w, "%s", "Cotnact admin!\n")
		log.Error("Method Not Allowed: %s", r.Method)
		return
	}

	if r.ParseForm() != nil {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, "%s", "Cotnact admin!\n")
		log.Error("Parse POST data failed!")
		return
	}

	if r.FormValue("postdata") == "" {
		w.WriteHeader(http.StatusExpectationFailed)
		fmt.Fprintf(w, "%s", "POST Data missing!\n")
		log.Error("POST Data missing")
		return
	}

	/*
	   Deciding file name
	*/
	const timeformat = "2006-01-02_15-04-05"
	var filename = time.Now().Format(timeformat) + ".log"
	if r.FormValue("username") != "" {
		filename = r.FormValue("username") + "_" + time.Now().Format(timeformat) + ".log"
	}

	/*
	   Json Parsing and data validation
	*/
	json.Unmarshal([]byte(r.FormValue("postdata")), &items)
	if len(items) == 0 {
		w.WriteHeader(http.StatusExpectationFailed)
		log.Error("Possible Json Parse Error: Nr of items: %d ", len(items))
		fmt.Fprintf(w, "Possible Json Parse Error: Nr of items: %d ", len(items))
		return
	}

	filebuffer := ""
	for _, item := range items {
		b, err := json.Marshal(item)
		if err != nil {
			log.Error("Json Parse Error: %s ", err)
		}
		filebuffer += string(b) + "\n"
	}

	log.Info("writing %d items to %s", len(items), filename)

	/*
	   File Creation
	*/
	f, err := os.Create(os.Args[2] + filename)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Error("File create error (%s): %s ", filename, err)
		return

	}
	/*
	   File Write
	*/
	_, err = io.WriteString(f, filebuffer)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Error("File write error (%s): %s ", filename, err)
		return

	}
	f.Close()

	m := email.NewMessage("Socialeyes feedback", "The feedback file is attached!")
	m.From = "*****@*****.**"
	m.To = []string{"*****@*****.**", "*****@*****.**"}
	error := m.Attach(os.Args[2] + filename)
	if error != nil {
		log.Info("Successfully sent notification mail.")
	}

	error = email.Send("smtp.gmail.com:587", smtp.PlainAuth("", "*****@*****.**", "<password>", "smtp.gmail.com"), m)

	fmt.Fprintf(w, "%s", filename)
	log.Info("Successfully wrote data to: " + filename)
	log.Close()
}
Example #8
0
func main() {
	log4go.AddFilter("stdout", log4go.FINE, log4go.NewConsoleLogWriter())
	log4go.AddFilter("filelog", log4go.FINE, log4go.NewFileLogWriter("beginner.log", false))

	route.Run()
}