Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 92af726

Browse filesBrowse files
committed
Updated revel to use new Logger
1 parent a28595b commit 92af726
Copy full SHA for 92af726

29 files changed

+981
-457
lines changed

‎binder.go

Copy file name to clipboardExpand all lines: binder.go
+16-14Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ type Binder struct {
4444
Unbind func(output map[string]string, name string, val interface{})
4545
}
4646

47+
var binderLog = RevelLog.New("section", "binder")
48+
4749
// ValueBinder is adapter for easily making one-key-value binders.
4850
func ValueBinder(f func(value string, typ reflect.Type) reflect.Value) func(*Params, string, reflect.Type) reflect.Value {
4951
return func(params *Params, name string, typ reflect.Type) reflect.Value {
@@ -82,7 +84,7 @@ var (
8284
}
8385
intValue, err := strconv.ParseInt(val, 10, 64)
8486
if err != nil {
85-
WARN.Println(err)
87+
binderLog.Warn("IntBinder Conversion Error", "error", err)
8688
return reflect.Zero(typ)
8789
}
8890
pValue := reflect.New(typ)
@@ -101,7 +103,7 @@ var (
101103
}
102104
uintValue, err := strconv.ParseUint(val, 10, 64)
103105
if err != nil {
104-
WARN.Println(err)
106+
binderLog.Warn("UintBinder Conversion Error", "error", err)
105107
return reflect.Zero(typ)
106108
}
107109
pValue := reflect.New(typ)
@@ -120,7 +122,7 @@ var (
120122
}
121123
floatValue, err := strconv.ParseFloat(val, 64)
122124
if err != nil {
123-
WARN.Println(err)
125+
binderLog.Warn("FloatBinder Conversion Error", "error", err)
124126
return reflect.Zero(typ)
125127
}
126128
pValue := reflect.New(typ)
@@ -296,7 +298,7 @@ func bindStruct(params *Params, name string, typ reflect.Type) reflect.Value {
296298
if params.JSON != nil {
297299
// Try to inject the response as a json into the created result
298300
if err := json.Unmarshal(params.JSON, resultPointer.Interface()); err != nil {
299-
WARN.Println("W: bindStruct: Unable to unmarshal request:", name, err)
301+
binderLog.Warn("bindStruct Unable to unmarshal request", "name", name, "error", err)
300302
}
301303
return result
302304
}
@@ -316,11 +318,11 @@ func bindStruct(params *Params, name string, typ reflect.Type) reflect.Value {
316318
// Time to bind this field. Get it and make sure we can set it.
317319
fieldValue := result.FieldByName(fieldName)
318320
if !fieldValue.IsValid() {
319-
WARN.Println("W: bindStruct: Field not found:", fieldName)
321+
binderLog.Warn("bindStruct Field not found", "name", fieldName)
320322
continue
321323
}
322324
if !fieldValue.CanSet() {
323-
WARN.Println("W: bindStruct: Field not settable:", fieldName)
325+
binderLog.Warn("bindStruct Field not settable", "name", fieldName)
324326
continue
325327
}
326328
boundVal := Bind(params, key[:len(name)+1+fieldLen], fieldValue.Type())
@@ -353,7 +355,7 @@ func getMultipartFile(params *Params, name string) multipart.File {
353355
if err == nil {
354356
return file
355357
}
356-
WARN.Println("Failed to open uploaded file", name, ":", err)
358+
binderLog.Warn("getMultipartFile: Failed to open uploaded file", "name", name, "error", err)
357359
}
358360
return nil
359361
}
@@ -372,7 +374,7 @@ func bindFile(params *Params, name string, typ reflect.Type) reflect.Value {
372374
// Otherwise, have to store it.
373375
tmpFile, err := ioutil.TempFile("", "revel-upload")
374376
if err != nil {
375-
WARN.Println("Failed to create a temp file to store upload:", err)
377+
binderLog.Warn("bindFile: Failed to create a temp file to store upload", "name", name, "error", err)
376378
return reflect.Zero(typ)
377379
}
378380

@@ -381,13 +383,13 @@ func bindFile(params *Params, name string, typ reflect.Type) reflect.Value {
381383

382384
_, err = io.Copy(tmpFile, reader)
383385
if err != nil {
384-
WARN.Println("Failed to copy upload to temp file:", err)
386+
binderLog.Warn("bindFile: Failed to copy upload to temp file", "name", name, "error", err)
385387
return reflect.Zero(typ)
386388
}
387389

388390
_, err = tmpFile.Seek(0, 0)
389391
if err != nil {
390-
WARN.Println("Failed to seek to beginning of temp file:", err)
392+
binderLog.Warn("bindFile: Failed to seek to beginning of temp file", "name", name, "error", err)
391393
return reflect.Zero(typ)
392394
}
393395

@@ -400,7 +402,7 @@ func bindByteArray(params *Params, name string, typ reflect.Type) reflect.Value
400402
if err == nil {
401403
return reflect.ValueOf(b)
402404
}
403-
WARN.Println("Error reading uploaded file contents:", err)
405+
binderLog.Warn("bindByteArray: Error reading uploaded file contents", "name", name, "error", err)
404406
}
405407
return reflect.Zero(typ)
406408
}
@@ -425,7 +427,7 @@ func bindMap(params *Params, name string, typ reflect.Type) reflect.Value {
425427
if params.JSON != nil {
426428
// Try to inject the response as a json into the created result
427429
if err := json.Unmarshal(params.JSON, resultPtr.Interface()); err != nil {
428-
WARN.Println("W: bindMap: Unable to unmarshal request:", name, err)
430+
binderLog.Warn("bindMap: Unable to unmarshal request", "name", name, "error", err)
429431
}
430432
return result
431433
}
@@ -472,7 +474,7 @@ func Unbind(output map[string]string, name string, val interface{}) {
472474
if binder.Unbind != nil {
473475
binder.Unbind(output, name, val)
474476
} else {
475-
ERROR.Printf("revel/binder: can not unbind %s=%s", name, val)
477+
binderLog.Error("Unbind: Unable to unmarshal request", "name", name, "value", val)
476478
}
477479
}
478480
}
@@ -482,7 +484,7 @@ func binderForType(typ reflect.Type) (Binder, bool) {
482484
if !ok {
483485
binder, ok = KindBinders[typ.Kind()]
484486
if !ok {
485-
WARN.Println("revel/binder: no binder for type:", typ)
487+
binderLog.Error("binderForType: no binder for type", "type", typ)
486488
return Binder{}, false
487489
}
488490
}

‎compress.go

Copy file name to clipboardExpand all lines: compress.go
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ var compressableMimes = [...]string{
3232
"application/x-javascript",
3333
}
3434

35+
// Local log instance for this class
36+
var compressLog = RevelLog.New("section", "compress")
37+
3538
// WriteFlusher interface for compress writer
3639
type WriteFlusher interface {
3740
io.Writer
@@ -75,7 +78,7 @@ func CompressFilter(c *Controller, fc []Filter) {
7578
c.Response.SetWriter(&writer)
7679
}
7780
} else {
78-
TRACE.Printf("Compression disabled for response status (%d)", c.Response.Status)
81+
compressLog.Debug("CompressFilter: Compression disabled for response ", "status", c.Response.Status)
7982
}
8083
}
8184
fc[0](c, fc[1:])
@@ -130,7 +133,7 @@ func (c *CompressResponseWriter) Close() error {
130133
c.Header.Del("Content-Length")
131134
if err := c.compressWriter.Close(); err != nil {
132135
// TODO When writing directly to stream, an error will be generated
133-
ERROR.Println("Error closing compress writer", c.compressionType, err)
136+
compressLog.Error("Close: Error closing compress writer", "type", c.compressionType, "error", err)
134137
}
135138

136139
}

‎controller.go

Copy file name to clipboardExpand all lines: controller.go
+16-13Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package revel
77
import (
88
"errors"
99
"fmt"
10+
"github.com/revel/revel/logger"
1011
"io"
1112
"net/http"
1213
"os"
@@ -15,7 +16,6 @@ import (
1516
"runtime"
1617
"strings"
1718
"time"
18-
"github.com/revel/revel/logger"
1919
)
2020

2121
// Controller Revel's controller structure that gets embedded in user defined
@@ -28,6 +28,7 @@ type Controller struct {
2828
AppController interface{} // The controller that was instantiated. embeds revel.Controller
2929
Action string // The fully qualified action name, e.g. "App.Index"
3030
ClientIP string // holds IP address of request came from
31+
module *Module // The module for the parent controller (if available)
3132

3233
Request *Request
3334
Response *Response
@@ -39,11 +40,12 @@ type Controller struct {
3940
Args map[string]interface{} // Per-request scratch space.
4041
ViewArgs map[string]interface{} // Variables passed to the template.
4142
Validation *Validation // Data validation helpers
42-
Log logger.MultiLogger // Context Logger
43+
Log logger.MultiLogger // Context Logger
4344
}
4445

4546
// The map of controllers, controllers are mapped by using the namespace|controller_name as the key
4647
var controllers = make(map[string]*ControllerType)
48+
var controllerLog = RevelLog.New("section", "controller")
4749

4850
// NewController returns new controller instance for Request and Response
4951
func NewControllerEmpty() *Controller {
@@ -159,7 +161,7 @@ func (c *Controller) Render(extraViewArgs ...interface{}) Result {
159161
// Get the calling function line number.
160162
_, _, line, ok := runtime.Caller(1)
161163
if !ok {
162-
ERROR.Println("Failed to get Caller information")
164+
controllerLog.Error("Render: Failed to get Caller information")
163165
}
164166

165167
// Get the extra ViewArgs passed in.
@@ -169,12 +171,12 @@ func (c *Controller) Render(extraViewArgs ...interface{}) Result {
169171
c.ViewArgs[renderArgNames[i]] = extraRenderArg
170172
}
171173
} else {
172-
ERROR.Println(len(renderArgNames), "RenderArg names found for",
173-
len(extraViewArgs), "extra ViewArgs")
174+
controllerLog.Error(fmt.Sprint(len(renderArgNames), "RenderArg names found for",
175+
len(extraViewArgs), "extra ViewArgs"))
174176
}
175177
} else {
176-
ERROR.Println("No RenderArg names found for Render call on line", line,
177-
"(Action", c.Action, ")")
178+
controllerLog.Error(fmt.Sprint("No RenderArg names found for Render call on line", line,
179+
"(Action", c.Action, ")"))
178180
}
179181

180182
return c.RenderTemplate(c.Name + "/" + c.MethodType.Name + "." + c.Request.Format)
@@ -241,6 +243,7 @@ func (c *Controller) RenderHTML(html string) Result {
241243
// action isn't done yet.
242244
func (c *Controller) Todo() Result {
243245
c.Response.Status = http.StatusNotImplemented
246+
controllerLog.Debug("Todo: Not implemented function", "action", c.Action)
244247
return c.RenderError(&Error{
245248
Title: "TODO",
246249
Description: "This action is not implemented",
@@ -285,7 +288,7 @@ func (c *Controller) RenderFile(file *os.File, delivery ContentDisposition) Resu
285288
fileInfo, err = file.Stat()
286289
)
287290
if err != nil {
288-
WARN.Println("RenderFile error:", err)
291+
controllerLog.Error("RenderFile: error", "error", err)
289292
}
290293
if fileInfo != nil {
291294
modtime = fileInfo.ModTime()
@@ -399,12 +402,12 @@ func ControllerTypeByName(controllerName string, moduleSource *Module) (c *Contr
399402
if c, found = controllers[controllerName]; !found {
400403
// Backup, passed in controllerName should be in lower case, but may not be
401404
if c, found = controllers[strings.ToLower(controllerName)]; !found {
402-
INFO.Printf("Cannot find controller name '%s' in controllers map ", controllerName)
405+
controllerLog.Debug("ControllerTypeByName: Cannot find controller in controllers map ", "controller", controllerName)
403406
// Search for the controller by name
404407
for _, cType := range controllers {
405408
testControllerName := strings.ToLower(cType.Type.Name())
406409
if testControllerName == strings.ToLower(controllerName) && (cType.ModuleSource == moduleSource || moduleSource == anyModule) {
407-
WARN.Printf("Matched empty namespace controller for %s to this %s", controllerName, cType.ModuleSource.Name)
410+
controllerLog.Warn("ControllerTypeByName: Matched empty namespace controller ", "controller", controllerName, "namespace", cType.ModuleSource.Name)
408411
c = cType
409412
found = true
410413
break
@@ -527,9 +530,9 @@ func AddControllerType(moduleSource *Module, controllerType reflect.Type, method
527530
controllers[newControllerType.ShortName()] = newControllerType
528531
}
529532
} else {
530-
ERROR.Printf("Error, attempt to register duplicate controller as %s", controllerName)
533+
controllerLog.Error("AddControllerType: Attempt to register duplicate controller as ", "controller", controllerName)
531534
}
532-
TRACE.Printf("Registered controller: %s", controllerName)
535+
controllerLog.Info("AddControllerType: Registered controller", "controller", controllerName)
533536

534537
return
535538
}
@@ -574,5 +577,5 @@ func RegisterController(c interface{}, methods []*MethodType) {
574577

575578
controllerType := AddControllerType(controllerModule, elem, methods)
576579

577-
TRACE.Printf("Registered controller: %s", controllerType.Name())
580+
controllerLog.Debug("RegisterController:Registered controller", "controller", controllerType.Name())
578581
}

‎field.go

Copy file name to clipboardExpand all lines: field.go
+26-7Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ import (
1313
type Field struct {
1414
Name string
1515
Error *ValidationError
16-
renderArgs map[string]interface{}
16+
viewArgs map[string]interface{}
17+
controller *Controller
1718
}
1819

19-
func NewField(name string, renderArgs map[string]interface{}) *Field {
20-
err, _ := renderArgs["errors"].(map[string]*ValidationError)[name]
20+
func NewField(name string, viewArgs map[string]interface{}) *Field {
21+
err, _ := viewArgs["errors"].(map[string]*ValidationError)[name]
22+
controller,_ := viewArgs["_controller"].(*Controller)
2123
return &Field{
2224
Name: name,
2325
Error: err,
24-
renderArgs: renderArgs,
26+
viewArgs: viewArgs,
27+
controller: controller,
2528
}
2629
}
2730

@@ -32,7 +35,7 @@ func (f *Field) ID() string {
3235

3336
// Flash returns the flashed value of this Field.
3437
func (f *Field) Flash() string {
35-
v, _ := f.renderArgs["flash"].(map[string]string)[f.Name]
38+
v, _ := f.viewArgs["flash"].(map[string]string)[f.Name]
3639
return v
3740
}
3841

@@ -48,7 +51,7 @@ func (f *Field) FlashArray() []string {
4851
// Value returns the current value of this Field.
4952
func (f *Field) Value() interface{} {
5053
pieces := strings.Split(f.Name, ".")
51-
answer, ok := f.renderArgs[pieces[0]]
54+
answer, ok := f.viewArgs[pieces[0]]
5255
if !ok {
5356
return ""
5457
}
@@ -70,10 +73,26 @@ func (f *Field) Value() interface{} {
7073
// ErrorClass returns ErrorCSSClass if this field has a validation error, else empty string.
7174
func (f *Field) ErrorClass() string {
7275
if f.Error != nil {
73-
if errorClass, ok := f.renderArgs["ERROR_CLASS"]; ok {
76+
if errorClass, ok := f.viewArgs["ERROR_CLASS"]; ok {
7477
return errorClass.(string)
7578
}
7679
return ErrorCSSClass
7780
}
7881
return ""
7982
}
83+
84+
// Get the short name and translate it
85+
func (f *Field) ShortName() string {
86+
name := f.Name
87+
if i:=strings.LastIndex(name,"."); i>0 {
88+
name = name[i+1:]
89+
}
90+
return f.Translate(name)
91+
}
92+
// Translate the text
93+
func (f *Field) Translate(text string, args...interface{}) string {
94+
if f.controller!=nil {
95+
text = f.controller.Message(text,args...)
96+
}
97+
return text
98+
}

‎http.go

Copy file name to clipboardExpand all lines: http.go
+9-8Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Request struct {
4141
}
4242

4343
var FORM_NOT_FOUND = errors.New("Form Not Found")
44+
var httpLog = RevelLog.New("section","http")
4445

4546
// Response is Revel's HTTP response object structure
4647
type Response struct {
@@ -152,7 +153,7 @@ type MultipartForm struct {
152153
origin ServerMultipartForm
153154
}
154155

155-
func (r *Request) MultipartReader() (*multipart.Reader, error) {
156+
func (req *Request) MultipartReader() (*multipart.Reader, error) {
156157

157158
return nil, errors.New("MultipartReader not supported, use controller.Param")
158159
}
@@ -224,11 +225,11 @@ func (r *Request) UserAgent() string {
224225
}
225226

226227
// Referer returns the client's Referer header string.
227-
func (r *Request) Referer() string {
228-
return r.ServerHeader.Get("Referer")
228+
func (req *Request) Referer() string {
229+
return req.ServerHeader.Get("Referer")
229230
}
230-
func (r *Request) GetHttpHeader(key string) string {
231-
return r.ServerHeader.Get(key)
231+
func (req *Request) GetHttpHeader(key string) string {
232+
return req.ServerHeader.Get(key)
232233
}
233234

234235
func (r *Request) GetValue(key int) (value interface{}) {
@@ -390,11 +391,11 @@ func (al AcceptLanguages) String() string {
390391
output := bytes.NewBufferString("")
391392
for i, language := range al {
392393
if _, err := output.WriteString(fmt.Sprintf("%s (%1.1f)", language.Language, language.Quality)); err != nil {
393-
ERROR.Println("WriteString failed:", err)
394+
httpLog.Error("String: WriteString failed:", "error", err)
394395
}
395396
if i != len(al)-1 {
396397
if _, err := output.WriteString(", "); err != nil {
397-
ERROR.Println("WriteString failed:", err)
398+
httpLog.Error("String: WriteString failed:","error", err)
398399
}
399400
}
400401
}
@@ -423,7 +424,7 @@ func ResolveAcceptLanguage(req *Request) AcceptLanguages {
423424
if qualifiedRange := strings.Split(languageRange, ";q="); len(qualifiedRange) == 2 {
424425
quality, err := strconv.ParseFloat(qualifiedRange[1], 32)
425426
if err != nil {
426-
WARN.Printf("Detected malformed Accept-Language header quality in '%s', assuming quality is 1", languageRange)
427+
httpLog.Warn("Detected malformed Accept-Language header quality in assuming quality is 1","languageRange", languageRange)
427428
acceptLanguages[i] = AcceptLanguage{qualifiedRange[0], 1}
428429
} else {
429430
acceptLanguages[i] = AcceptLanguage{qualifiedRange[0], float32(quality)}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.