Example #1
0
func ExampleLogger_ToConsole() {
	// Simply log to console.
	ol.Info.Println(nil, "The log text.")
	ol.Trace.Println(nil, "The log text.")
	ol.Warn.Println(nil, "The log text.")
	ol.Error.Println(nil, "The log text.")

	// Use short aliases.
	ol.I(nil, "The log text.")
	ol.T(nil, "The log text.")
	ol.W(nil, "The log text.")
	ol.E(nil, "The log text.")
}
Example #2
0
// http standard error response.
// @remark for not SystemError, we will use logger.E to print it.
// @remark user can use WriteError() for simple api.
func Error(ctx ol.Context, err error) http.Handler {
	// for complex error, use code instead.
	if v, ok := err.(SystemComplexError); ok {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			ol.E(ctx, "Serve", r.URL, "failed. err is", err.Error())
			jsonHandler(ctx, v).ServeHTTP(w, r)
		})
	}

	// for int error, use code instead.
	if v, ok := err.(SystemError); ok {
		return jsonHandler(ctx, map[string]int{"code": int(v)})
	}

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		SetHeader(w)
		w.Header().Set("Content-Type", HttpJson)

		// unknown error, log and response detail
		http.Error(w, err.Error(), http.StatusInternalServerError)
		ol.E(ctx, "Serve", r.URL, "failed. err is", err.Error())
	})
}
Example #3
0
func (v *aspContextNoExit) WatchParent() {
	ppid := os.Getppid()

	go func() {
		for {
			if pid := os.Getppid(); pid == 1 || pid != ppid {
				ol.E(v.ctx, "quit for parent problem, ppid is", pid)
				select {
				case v.quit <- true:
				default:
				}
				break
			}
			//ol.T(v.ctx, "parent pid", ppid, "ok")

			time.Sleep(v.interval)
		}
	}()
	ol.T(v.ctx, "parent process watching, ppid is", ppid)
}
Example #4
0
func (v *aspContext) WatchParent() {
	ppid := os.Getppid()

	go func() {
		for {
			if pid := os.Getppid(); pid == 1 || pid != ppid {
				ol.E(v.ctx, "quit for parent problem, ppid is", pid)

				if v.callback != nil {
					v.callback()
				}

				os.Exit(0)
			}
			//ol.T(v.ctx, "parent pid", ppid, "ok")

			time.Sleep(v.interval)
		}
	}()
	ol.T(v.ctx, "parent process watching, ppid is", ppid)
}