Do you call B and C with any arguments?
if you have call B and C with req, you can use the method: req.WithContext
as below:
func A(w http.ResponseWriter, req *http.Request) {
ctx := Context.WithValue(req.Context(), "log", log)
req := req.WithContext(ctx)
B(req)
C(req)
}
func B(req) {
* log := req.Value("log"*
* log.Info("im in func B!")*
// ...
}
On Wednesday, February 15, 2012 at 4:32:45 PM UTC+8, 韩拓 wrote:
>
> hi list,
> my problem is, i have a typical http server,like:
> func A(w http.ResponseWriter, req *http.Request) {
> B()
> C()
> }
>
> func B() {
> // ...
> }
> func C() {
> // ...
> }
>
> now i want to log something (per request),like:
> func A(w http.ResponseWriter, req *http.Request) {
> *log := NewLog()*
> *log.Info("call B")*
> B()
> * log.Info("call C")*
> C()
> *if (apiDebug) { ResponseLog(w, log) }*
> }
>
> func B() {
> // ...
> }
> func C() {
> // ...
> }
>
> the problem is i can`t log in func B or func C,unless modify their
> signature :
> func B(log Log) or func B(cxt Context)
> but there are lots of codes,nearly all my functions need modify.
>
> so, if i can get the current goroutine`s id or its local storage(if
> have),i can do this:
>
> func A(w http.ResponseWriter, req *http.Request) {
> *SetContext(xxx) // or SetContext(xxx, myGoroutineId)*
> B()
> C()
> *ClearContext(myGoroutineId)*
> }
>
> func B() {
> * log := GetContext() // or GetContext(myGoroutineId)*
> * log.Info("im in func B!")*
> // ...
> }
> func C() {
> // ...
> }
>
> could i do this in golang?
>
> thanks.
>
> --
> 此致,
> 敬礼!
>
> 韩拓
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.