log: new interface

New logging interface simplifies and generalizes.

1) Loggers now have only one output.
2) log.Stdout, Stderr, Crash and friends are gone.
	Logging is now always to standard error by default.
3) log.Panic* replaces log.Crash*.
4) Exiting and panicking are not part of the logger's state; instead
	the functions Exit* and Panic* simply call Exit or panic after
	printing.
5) There is now one 'standard logger'.  Instead of calling Stderr,
	use Print etc.  There are now triples, by analogy with fmt:
		Print, Println, Printf
	What was log.Stderr is now best represented by log.Println,
	since there are now separate Print and Println functions
	(and methods).
6) New functions SetOutput, SetFlags, and SetPrefix allow global
	editing of the standard logger's properties.   This is new
	functionality. For instance, one can call
		log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds)
	to get all logging output to show file name, line number, and
	time stamp.

In short, for most purposes
	log.Stderr -> log.Println or log.Print
	log.Stderrf -> log.Printf
	log.Crash -> log.Panicln or log.Panic
	log.Crashf -> log.Panicf
	log.Exit -> log.Exitln or log.Exit
	log.Exitf -> log.Exitf (no change)

This has a slight breakage: since loggers now write only to one
output, existing calls to log.New() need to delete the second argument.
Also, custom loggers with exit or panic properties will need to be
reworked.

All package code updated to new interface.

The test has been reworked somewhat.

The old interface will be removed after the new release.
For now, its elements are marked 'deprecated' in their comments.

Fixes #1184.

R=rsc
CC=golang-dev
https://golang.org/cl/2419042
This commit is contained in:
Rob Pike 2010-10-12 12:59:18 -07:00
parent d687ea5588
commit 12da5a90e0
36 changed files with 358 additions and 303 deletions

View file

@ -197,7 +197,7 @@ func (server *serverType) register(rcvr interface{}) os.Error {
}
if s.typ.PkgPath() != "" && !isPublic(sname) {
s := "rpc Register: type " + sname + " is not public"
log.Stderr(s)
log.Print(s)
return os.ErrorString(s)
}
if _, present := server.serviceMap[sname]; present {
@ -216,41 +216,41 @@ func (server *serverType) register(rcvr interface{}) os.Error {
}
// Method needs three ins: receiver, *args, *reply.
if mtype.NumIn() != 3 {
log.Stderr("method", mname, "has wrong number of ins:", mtype.NumIn())
log.Println("method", mname, "has wrong number of ins:", mtype.NumIn())
continue
}
argType, ok := mtype.In(1).(*reflect.PtrType)
if !ok {
log.Stderr(mname, "arg type not a pointer:", mtype.In(1))
log.Println(mname, "arg type not a pointer:", mtype.In(1))
continue
}
replyType, ok := mtype.In(2).(*reflect.PtrType)
if !ok {
log.Stderr(mname, "reply type not a pointer:", mtype.In(2))
log.Println(mname, "reply type not a pointer:", mtype.In(2))
continue
}
if argType.Elem().PkgPath() != "" && !isPublic(argType.Elem().Name()) {
log.Stderr(mname, "argument type not public:", argType)
log.Println(mname, "argument type not public:", argType)
continue
}
if replyType.Elem().PkgPath() != "" && !isPublic(replyType.Elem().Name()) {
log.Stderr(mname, "reply type not public:", replyType)
log.Println(mname, "reply type not public:", replyType)
continue
}
if mtype.NumIn() == 4 {
t := mtype.In(3)
if t != reflect.Typeof((*ClientInfo)(nil)) {
log.Stderr(mname, "last argument not *ClientInfo")
log.Println(mname, "last argument not *ClientInfo")
continue
}
}
// Method needs one out: os.Error.
if mtype.NumOut() != 1 {
log.Stderr("method", mname, "has wrong number of outs:", mtype.NumOut())
log.Println("method", mname, "has wrong number of outs:", mtype.NumOut())
continue
}
if returnType := mtype.Out(0); returnType != typeOfOsError {
log.Stderr("method", mname, "returns", returnType.String(), "not os.Error")
log.Println("method", mname, "returns", returnType.String(), "not os.Error")
continue
}
s.method[mname] = &methodType{method: method, argType: argType, replyType: replyType}
@ -258,7 +258,7 @@ func (server *serverType) register(rcvr interface{}) os.Error {
if len(s.method) == 0 {
s := "rpc Register: type " + sname + " has no public methods of suitable type"
log.Stderr(s)
log.Print(s)
return os.ErrorString(s)
}
server.serviceMap[s.name] = s
@ -289,7 +289,7 @@ func sendResponse(sending *sync.Mutex, req *Request, reply interface{}, codec Se
sending.Lock()
err := codec.WriteResponse(resp, reply)
if err != nil {
log.Stderr("rpc: writing response: ", err)
log.Println("rpc: writing response:", err)
}
sending.Unlock()
}
@ -344,7 +344,7 @@ func (server *serverType) input(codec ServerCodec) {
if err != nil {
if err == os.EOF || err == io.ErrUnexpectedEOF {
if err == io.ErrUnexpectedEOF {
log.Stderr("rpc: ", err)
log.Println("rpc:", err)
}
break
}
@ -378,7 +378,7 @@ func (server *serverType) input(codec ServerCodec) {
replyv := _new(mtype.replyType)
err = codec.ReadRequestBody(argv.Interface())
if err != nil {
log.Stderr("rpc: tearing down", serviceMethod[0], "connection:", err)
log.Println("rpc: tearing down", serviceMethod[0], "connection:", err)
sendResponse(sending, req, replyv.Interface(), codec, err.String())
break
}
@ -454,7 +454,7 @@ func serveHTTP(w http.ResponseWriter, req *http.Request) {
}
conn, _, err := w.Hijack()
if err != nil {
log.Stderr("rpc hijacking ", w.RemoteAddr(), ": ", err.String())
log.Print("rpc hijacking ", w.RemoteAddr(), ": ", err.String())
return
}
io.WriteString(conn, "HTTP/1.0 "+connected+"\n\n")