![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
access-log
Advanced tools
Add simple access logs to any http or https server
var http = require('http');
var accesslog = require('access-log');
http.createServer(function(req, res) {
accesslog(req, res);
res.end();
}).listen(80, '0.0.0.0');
This will automatically log requests as they come in to the web server that look like...
127.0.0.1 - - [13/Sep/2013:01:38:09 -0400] "GET / HTTP/1.1" 200 - "-" "-"
127.0.0.1 - - [13/Sep/2013:01:38:09 -0400] "GET /testing HTTP/1.1" 200 - "-" "-"
127.0.0.1 - - [13/Sep/2013:01:38:10 -0400] "GET /index.html HTTP/1.1" 200 - "-" "-"
Opts is an object that can contain a format identifier and userID func (both optional).
For example,
{
userID: function (req) { return req.user; },
format : 'url=":url" method=":method" statusCode=":statusCode" delta=":delta" ip=":ip"'
}
You can pass in a format string, the default is Apache Common Log Format http://en.wikipedia.org/wiki/Common_Log_Format
If opts
is a string, it is assumed to be the opts.format
property.
:ip - :userID [:clfDate] ":method :url :protocol/:httpVersion" :statusCode :contentLength ":referer" ":userAgent"
clfDate
: The date of the end of the response in Apache Common Log formatcontentLength
: The response Content-Length
header, or -
if unsetdelta
: The time in ms from request to responseendDate
: The ISO formatted string when the response was endedendTime
: The epoch time when the response was endedhost
: The host header from the request if sethttpVersion
: The HTTP version used (ie. 1.0
, 1.1
)ip
: The remote IPXip
: The remote IP, using X-Forwarded-For
if setmethod
: The HTTP methodprotocol
: HTTP
or HTTPS
referer
: The request Referer
header, or -
if unsetstartDate
: The ISO formatted string when the request was receivedstartTime
: The epoch time when the request was receivedstatusCode
: The response status code sent from the serverurl
: The requested URLurlDecoded
: The decoded request URL (ie. %20
=>
)userID
: The username if applicableuserAgent
: The request User-Agent
header, or -
if unsetNOTE: Wrap variables in {}
to protect against unwanted interpolation.
ex:
request to :url took :{delta}ms
You can also pass in your own custom callback, the default is console.log
.
The only argument passed is the access log string
var format = 'url=":url" method=":method" statusCode=":statusCode" delta=":delta" ip=":ip"';
accesslog(req, res, format, function(s) {
console.log(s);
});
yields
url="/projects" method="GET" statusCode="200" delta="0" ip="127.0.0.1"
url="/testing" method="GET" statusCode="200" delta="1" ip="127.0.0.1"
url="/index.html" method="GET" statusCode="200" delta="0" ip="127.0.0.1"
npm install access-log
Consider further customizing the access logs by using the [log-timestamp] (https://github.com/bahamas10/node-log-timestamp) module to prepend a timestamp automatically.
MIT Licensed
FAQs
Add simple access logs to any http or https server
The npm package access-log receives a total of 2,515 weekly downloads. As such, access-log popularity was classified as popular.
We found that access-log demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.