ExpressJS:在每个Log语句中包括会话ID [英] ExpressJS: Include Session ID in every Log statement

查看:88
本文介绍了ExpressJS:在每个Log语句中包括会话ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在我的一个项目中使用Express 4和Node 8.我希望能够记录该应用程序中发生的某些情况,并让会话ID充当将一连串的日志链接在一起的方式.我正在使用Winston进行日志记录.

I am currently using Express 4 and Node 8 in a project of mine. I want to be able to log certain things that occur in the app and have the session ID serve as the way to link a chain of logs together. I am using winston for logging.

问题:如何从应用程序中的任何位置访问此会话ID,以便可以将其包含在日志中?

The Question: How can I access this session ID from anywhere in the app so that it can be included in logs?

我知道您不能仅将会话ID设置为全局变量,因为传入的后续请求将覆盖设置的任何内容.

I understand that you can't just set the session id to a global variable since incoming subsequent requests will override whatever is set.

此外,我知道可以将会话ID从路由层向下传递到所有其他需要它的功能,但是当最深层的功能仅需要用于记录日志的会话ID时,这会很麻烦.

Additionally, I know it is possible to pass the session id down from the routing layer to all other functions that need it, but this can be rather cumbersome when functions at the deepest levels need the session id just for logging.

对于多线程应用程序,每个线程可以拥有自己的内存池,以便可以安全地全局容纳日志记录相关ID,而不会受到其他请求的干扰.但是,由于Node是单线程的,因此除非我缺少某些东西,否则这似乎是不可能的.

For multithreaded applications, each thread can have its own memory pool so that it can safely house a logging correlation id globally without interference from other requests. However, since Node is single-threaded, this does not seem to be possible, unless I am missing something.

是否有一种方法可以拥有一个特定于请求的全局池,或者有更好的方法在每个生成的日志语句中包含一个关联ID?

Is there a way to have a request-specific global pool, or perhaps is there a better way to include a correlation id in every log statement that is made?

推荐答案

问题:如何从应用程序中的任何位置访问此会话ID,以便可以将其包含在日志中?

The Question: How can I access this session ID from anywhere in the app so that it can be included in logs?

传入请求中的req对象是特定于请求的对象,该对象将具有当前请求的状态(包括会话信息).在node.js中,实际上没有其他方法可以执行此操作.如您所知,由于node.js具有共享的单线程体系结构,因此不存在诸如特定于请求的全局变量之类的东西,您可以在其中放置可从任何地方访问的内容,它会告诉您什么会话重新投放.

The req object from an incoming request IS the request-specific object that will have the state for the current request (including session information). There is really no other way to do things in node.js. As you seem to already know, because of the shared, single thread architecture of node.js, there are no such things as request-specific globals where you could put something that you could access from anywhere and it would tell you what session you're serving.

如果要访问会话状态,则必须将req对象或会话状态本身传递到要记录它的级别.真的没有别的做事方式.

If you want access to the session state, then you have to pass the req object or the session state itself down to the level where you want to log it. There just really isn't another way to do things.

我知道您不能仅将会话ID设置为全局变量,因为传入的后续请求将覆盖设置的任何内容.

I understand that you can't just set the session id to a global variable since incoming subsequent requests will override whatever is set.

正确.试图将任何特定于请求的状态放到全局空间中,只会导致对它们进行交织的几个不同请求的协作事件处理定期将其踩踏.

Correct. Trying to put any request-specific state in a global space will just cause it to regularly get tromped on by the cooperative event processing of several different requests interleaving their work.

对于多线程应用程序,每个线程可以拥有自己的内存池,以便可以安全地全局存储日志记录相关ID,而不会受到其他请求的干扰.但是,由于Node是单线程的,因此除非我缺少某些东西,否则这似乎是不可能的.

For multithreaded applications, each thread can have its own memory pool so that it can safely house a logging correlation id globally without interference from other requests. However, since Node is single-threaded, this does not seem to be possible, unless I am missing something.

您什么都不会错过.

是否有一种方法可以拥有一个特定于请求的全局池,或者有更好的方法在每个生成的日志语句中包含一个关联ID?

Is there a way to have a request-specific global pool, or perhaps is there a better way to include a correlation id in every log statement that is made?

不,没有.您只需要传递req对象或将特定于会话的状态传递到要登录的级别.没有特定于请求的全局池. node.js不会保持某种状态,即哪个请求在任何给定时间都在运行代码.您可能已经知道,一个请求可以开始处理,然后在等待异步操作的同时将控制权返回给系统,另一个请求开始处理,然后在等待异步操作的同时返回控制的请求,前者获取一个事件并开始执行代码.当随后的异步事件导致新的代码在执行请求的服务时再次执行时,堆栈将再次为空(其上没有请求特定的状态).

No, there is not. You just have to pass the req object or pass session-specific state down to the level you want to log it at. There is no request-specific global pool. node.js does not maintain some sort of state about which request has code running at any given time. As you probably already know, one request can start processing, then return control back to the system while it awaits some async operation and another starts processing and then when that one returns control while it awaits some async operation, the former gets an event and starts executing code. When subsequent async events cause new code in service of a request to be executed again, the stack is empty again (there is no request-specific state on it).

使之成为可能或成立的可能关键与代码的结构,在较低级别上可用的对象有关,因此您可以确定可以将会话信息附加到哪些对象上,从而使其可以在您所在的位置访问需要它.这不是通用的解决方案,而是取决于代码的确切布局和结构,因此,在没有看到您要记住的特定代码的情况下,除了将会话信息传递到所需的位置之外",我们无法提供其他更具体的建议它".

The likely key to making this possible or tenable has to do with how your code is structured, what objects are available at a fairly low level so you can figure out what object you can attach session info to that makes it accessible where you need it. This is not a generic solution, but depends upon the exact layout and structure of your code so without seeing the specific code you have in mind, we can't really advise any more specifically other than "pass the session info down to where you need it".

这篇关于ExpressJS:在每个Log语句中包括会话ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆