Ejemplo n.º 1
0
func main() {
	pflag.Usage = func() {
		fmt.Printf(`usage: tuplespaced [flags]

Run tuplespace server.

Flags:
`)
		pflag.PrintDefaults()
	}
	pflag.Parse()
	runtime.GOMAXPROCS(*ncpuFlag)

	log.AddFilter("stdout", logLevels[*logLevelFlag], log.NewConsoleLogWriter())
	debug := logLevels[*logLevelFlag] <= log.DEBUG

	log.Info("Starting server on http://%s/tuplespace/", *bindFlag)

	ts := tuplespace.NewTupleSpace(store.NewMemoryStore())

	srv := &http.Server{
		Addr:         *bindFlag,
		Handler:      makeService(ts, debug),
		ReadTimeout:  *readTimeoutFlag,
		WriteTimeout: *writeTimeoutFlag,
	}
	err := srv.ListenAndServe()
	if err != nil {
		fatalf("error: %s\n", err)
	}
}
Ejemplo n.º 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, "日志模块已启动。")
}
Ejemplo n.º 3
0
Archivo: env.go Proyecto: OldBao/raft
func Init(cpath, file string) error {
	Log = make(l4g.Logger)

	Log.AddFilter("stdout", l4g.DEBUG, l4g.NewConsoleLogWriter())
	//Log.AddFilter("log", log4go.FINE, log4go.NewFileLogWriter("example.log", true))

	if err := gcfg.ReadFileInto(&Conf, path.Join(cpath, file)); err != nil {
		fmt.Println("Load Conf error : ", err)
		return err
	}

	return nil
}
Ejemplo n.º 4
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)
}
Ejemplo n.º 5
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)
	}
}
Ejemplo n.º 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)
	}
}
Ejemplo n.º 7
0
func main() {
	runtime.GOMAXPROCS(1)

	loglevel := log4go.INFO
	if os.Getenv("LOG_LEVEL") == "debug" {
		loglevel = log4go.DEBUG
	}

	logger = make(log4go.Logger)
	w := log4go.NewConsoleLogWriter()
	w.SetFormat("[%D %T] [%L] %M")
	logger.AddFilter("stdout", loglevel, w)

	checking = make(map[string]bool)

	loggerAddr = os.Getenv("LOGGER_ADDR")
	if loggerAddr == "" {
		logger.Critical("no logger address found")
		logger.Close()
		return
	}

	loggerIndex = os.Getenv("LOGGER_INDEX")
	if loggerIndex == "" {
		loggerIndex = "logstash-docker"
	}

	client = &http.Client{}

	logger.Info("setup statsd client to %s", loggerAddr)

	for {
		logger.Debug("listing all containers")

		checkConteiners()
		time.Sleep(5000 * time.Millisecond)
	}
}
Ejemplo n.º 8
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()
}
Ejemplo n.º 9
0
func main() {
	log4go.AddFilter("stdout", log4go.FINE, log4go.NewConsoleLogWriter())
	log4go.AddFilter("filelog", log4go.FINE, log4go.NewFileLogWriter("beginner.log", false))

	route.Run()
}
Ejemplo n.º 10
0
func init() {
	log.AddFilter("stdout", log.WARNING, log.NewConsoleLogWriter())
}