本文整理匯總了Golang中github.com/labstack/gommon/log.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestEcho
func TestEcho(t *testing.T) {
e := New()
req, _ := http.NewRequest(GET, "/", nil)
rec := httptest.NewRecorder()
c := NewContext(req, NewResponse(rec, e), e)
// Router
assert.NotNil(t, e.Router())
// Debug
e.SetDebug(true)
assert.True(t, e.debug)
// DefaultHTTPErrorHandler
e.DefaultHTTPErrorHandler(errors.New("error"), c)
assert.Equal(t, http.StatusInternalServerError, rec.Code)
// Logger
l := log.New("test")
e.SetLogger(l)
assert.Equal(t, l, e.Logger())
// Autoindex
e.AutoIndex(true)
assert.True(t, e.autoIndex)
}
示例2: New
// New creates an instance of Echo.
func New() (e *Echo) {
e = &Echo{maxParam: new(int)}
e.pool.New = func() interface{} {
return NewContext(nil, new(Response), e)
}
e.router = NewRouter(e)
//----------
// Defaults
//----------
e.HTTP2(true)
e.defaultHTTPErrorHandler = func(err error, c *Context) {
code := http.StatusInternalServerError
msg := http.StatusText(code)
if he, ok := err.(*HTTPError); ok {
code = he.code
msg = he.message
}
if e.debug {
msg = err.Error()
}
if !c.response.committed {
http.Error(c.response, msg, code)
}
e.logger.Error(err)
}
e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler)
e.SetBinder(&binder{})
// Logger
e.logger = log.New("echo")
return
}
示例3: TestResponseWriteHeader
func TestResponseWriteHeader(t *testing.T) {
rec := httptest.NewRecorder()
res := NewResponse(rec, log.New("test"))
res.WriteHeader(http.StatusOK)
assert.True(t, res.Committed())
assert.Equal(t, http.StatusOK, res.Status())
}
示例4: NewConfig
func NewConfig(c *engine.Config) (s *Server) {
s = &Server{
Server: new(http.Server),
config: c,
pool: &Pool{
request: sync.Pool{
New: func() interface{} {
return &Request{}
},
},
response: sync.Pool{
New: func() interface{} {
return &Response{logger: s.logger}
},
},
header: sync.Pool{
New: func() interface{} {
return &Header{}
},
},
url: sync.Pool{
New: func() interface{} {
return &URL{}
},
},
},
handler: engine.HandlerFunc(func(req engine.Request, res engine.Response) {
s.logger.Fatal("handler not set")
}),
logger: log.New("echo"),
}
return
}
示例5: NewFromConfig
// NewFromConfig returns an instance of `standard.Server` from config.
func NewFromConfig(c engine.Config) (s *Server) {
s = &Server{
Server: new(http.Server),
config: c,
pool: &pool{
request: sync.Pool{
New: func() interface{} {
return &Request{logger: s.logger}
},
},
response: sync.Pool{
New: func() interface{} {
return &Response{logger: s.logger}
},
},
header: sync.Pool{
New: func() interface{} {
return &Header{}
},
},
url: sync.Pool{
New: func() interface{} {
return &URL{}
},
},
},
handler: engine.HandlerFunc(func(rq engine.Request, rs engine.Response) {
s.logger.Error("handler not set, use `SetHandler()` to set it.")
}),
logger: log.New("echo"),
}
s.Addr = c.Address
s.Handler = s
return
}
示例6: TestLoggerIPAddress
func TestLoggerIPAddress(t *testing.T) {
e := echo.New()
l := log.New("echo")
buf := new(bytes.Buffer)
l.SetOutput(buf)
e.SetLogger(l)
req, _ := http.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := echo.NewContext(req, echo.NewResponse(rec, e), e)
ip := "127.0.0.1"
h := func(c *echo.Context) error {
return c.String(http.StatusOK, "test")
}
mw := Logger()
// With X-Real-IP
req.Header.Add(echo.XRealIP, ip)
mw(h)(c)
assert.Contains(t, buf.String(), ip)
// With X-Forwarded-For
buf.Reset()
req.Header.Del(echo.XRealIP)
req.Header.Add(echo.XForwardedFor, ip)
mw(h)(c)
assert.Contains(t, buf.String(), ip)
// with req.RemoteAddr
buf.Reset()
mw(h)(c)
assert.Contains(t, buf.String(), ip)
}
示例7: TestResponseWrite
func TestResponseWrite(t *testing.T) {
c := new(fasthttp.RequestCtx)
res := NewResponse(c, log.New("test"))
res.Write([]byte("test"))
assert.Equal(t, int64(4), res.Size())
assert.Equal(t, "test", string(c.Response.Body()))
}
示例8: TestResponseWriteHeader
func TestResponseWriteHeader(t *testing.T) {
c := new(fasthttp.RequestCtx)
res := NewResponse(c, log.New("test"))
res.WriteHeader(http.StatusOK)
assert.True(t, res.Committed())
assert.Equal(t, http.StatusOK, res.Status())
}
示例9: TestRequest
func TestRequest(t *testing.T) {
ctx := new(fast.RequestCtx)
url, _ := url.Parse("http://github.com/labstack/echo")
ctx.Init(&fast.Request{}, fakeAddr{addr: "127.0.0.1"}, nil)
ctx.Request.Read(bufio.NewReader(bytes.NewBufferString(test.MultipartRequest)))
ctx.Request.SetRequestURI(url.String())
test.RequestTest(t, NewRequest(ctx, log.New("echo")))
}
示例10: TestResponseSetCookie
func TestResponseSetCookie(t *testing.T) {
rec := httptest.NewRecorder()
res := NewResponse(rec, log.New("test"))
res.SetCookie(&Cookie{&http.Cookie{
Name: "name",
Value: "Jon Snow",
}})
assert.Equal(t, "name=Jon Snow", rec.Header().Get("Set-Cookie"))
}
示例11: TestResponseWrite
func TestResponseWrite(t *testing.T) {
rec := httptest.NewRecorder()
res := NewResponse(rec, log.New("test"))
res.Write([]byte("test"))
assert.Equal(t, int64(4), res.Size())
assert.Equal(t, "test", rec.Body.String())
res.Flush()
assert.True(t, rec.Flushed)
}
示例12: New
func New(templateDir string) tplex.TemplateEx {
a := &templatePongo2{
templateDir: templateDir,
ext: `.html`,
Logger: log.New("tplex"),
}
a.templateDir, _ = filepath.Abs(templateDir)
return a
}
示例13: TestEchoLogger
func TestEchoLogger(t *testing.T) {
e := New()
l := log.New("test")
e.SetLogger(l)
assert.Equal(t, l, e.Logger())
e.SetLogOutput(ioutil.Discard)
assert.Equal(t, l.Output(), ioutil.Discard)
e.SetLogLevel(log.OFF)
assert.Equal(t, l.Level(), log.OFF)
}
示例14: NewResponseRecorder
func NewResponseRecorder() *ResponseRecorder {
rec := httptest.NewRecorder()
return &ResponseRecorder{
Response: &Response{
response: rec,
header: &Header{rec.Header()},
writer: rec,
logger: log.New("test"),
},
Body: rec.Body,
}
}
示例15: TestResponseSetCookie
func TestResponseSetCookie(t *testing.T) {
c := new(fasthttp.RequestCtx)
res := NewResponse(c, log.New("test"))
cookie := new(fasthttp.Cookie)
cookie.SetKey("name")
cookie.SetValue("Jon Snow")
res.SetCookie(&Cookie{cookie})
c.Response.Header.SetCookie(cookie)
ck := new(fasthttp.Cookie)
ck.SetKey("name")
assert.True(t, c.Response.Header.Cookie(ck))
assert.Equal(t, "Jon Snow", string(ck.Value()))
}