-
Notifications
You must be signed in to change notification settings - Fork 458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Visualises requests when in development. #104
Conversation
@@ -37,6 +55,12 @@ async function run(req, res, fn, onError) { | |||
|
|||
async function json(req, {limit = '1mb'} = {}) { | |||
try { | |||
if (DEV && req.parsedJson) { | |||
return req.parsedJson | |||
} else if (DEV && req.jsonError) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we first check if there is an error? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In theory, there can only be one or the other, but I can switch the order if you like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine 👍, maybe add in a way to pass a custom parse function later on, so you can also log out other stuff. urlencoded forms for example.
For now this will do fine 😄
This is really beautiful @rickharrison. I think we should have The prod mode of this would be a compressed version that excludes the body. |
(basically something similar to the default express logger) |
Sounds good! I will work on this over my holiday break :) |
This is amazing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good :)
@rauchg final check? |
I'll take a look! |
-H Host to listen on [0.0.0.0] | ||
-p Port to listen on [3000] | ||
-h Show this help message | ||
--log dev|prod|none`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
off
would be better than none
IMO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we also need long forms for all of this, in addition to the short form
-H, --host
-p, --port
-h, --help
-L, --log
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add your second comment to my readme PR @rauchg
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -5,8 +5,22 @@ const server = require('http').Server | |||
const getRawBody = require('raw-body') | |||
const typer = require('media-typer') | |||
const isStream = require('isstream') | |||
const chalk = require('chalk') | |||
const jsome = require('jsome') | |||
|
|||
const DEV = 'development' === process.env.NODE_ENV |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be cool to document in the README that we do the nicer logging upon explicit NODE_ENV = development
setting
@@ -41,6 +63,14 @@ async function run(req, res, fn, onError) { | |||
|
|||
async function json(req, {limit = '1mb'} = {}) { | |||
try { | |||
if (req.jsonError) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to avoid polluting req
if at all possible
@@ -250,6 +250,14 @@ function handleErrors (fn) { | |||
} | |||
``` | |||
|
|||
### Logging | |||
|
|||
Micro provides some built-in logging to help visualise requests. When `NODE_ENV` is set to `development`, micro will display the request along with any json body like this: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove some
@@ -28,7 +29,8 @@ const help = () => { | |||
console.log(`Usage: micro [opts] <file> | |||
-H, --host Host to listen on [0.0.0.0] | |||
-p, --port Port to listen on [3000] | |||
-h, --help Show this help message`); | |||
-h, --help Show this help message | |||
-L, --log dev|prod|off`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turned this around in my merge commit. First the logging then help, also added [off]
as default indicator
@rauchg 🚀 |
@rickharrison @timneutkens I think a few additions would make sense:
|
Sounds good! I'll take a look at this later tonight. |
@rauchg So I have it logging response objects as well now, but I did it by adding a property to The problem is I'm not sure how to get the response body in the As another caveat, if I was unable to find a way to align the time text to the right side. If anyone else has an idea of how to do that with ANSI, please let me know. Happy to chat more on the zeit slack about this as well! EDIT: Updated screenshot |
that looks really amazing! One way we could capture the response I assume is by patching the internal write method of the response object? The one that both |
Yea, we definitely could do that. I can take a stab and patching those methods inside of the |
Also, I have a slightly different take on this now: {
"dependencies": {
"micro": "latest"
},
"devDependencies": {
"micro-dev": "latest"
}
}
This way, |
@rauchg The repository is empty -- forgot to push or do you need a PR? 😊 |
I'm working on it @onbjerg 😄 |
@timneutkens I couldn't wait so I implemented it as a simple wrapper function instead of a CLI tool: https://github.com/onbjerg/micro-visualize |
@onbjerg micro-dev is most likely coming today :) |
https://github.com/onbjerg/micro-visualize is still an option |
I took a stab at visualising requests from #33. The one thing that is a little messy is parsing and displaying the json. I ended up keeping a reference to it on the request since you can only call
getRawBody
once per request.Let me know your thoughts!