Merge branch 'qnkhuat:main' into custom

This commit is contained in:
Philipp Dieter 2021-09-04 14:29:48 +02:00 committed by GitHub
commit d3739cc2d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 19 deletions

View File

@ -1,13 +1,14 @@
# Termlog
Bring browser console to your terminal
Console log to terminal
### What it does
termlog send the browser console log to your terminal.
termlog send the browser console log to your terminal
It also comes with a __nodejs__ REPL so you can do some basic draft code
### When to use it
While you developing your front-end app and you have to switch back and forth between IDE and browser.
- While you developing your front-end app and you have to switch back and forth between IDE and browser
- When you test app on mobile and need to check log. (See [Debug on mobile](#debug-on-mobile))
# How to use it?
There are 2 ways and it depends on your preferences
@ -21,11 +22,11 @@ There are 2 ways and it depends on your preferences
import termlog from "termlog"
termlog()
```
4. You should now see log being streamed to your terminal
5. You should now see log being streamed to your terminal
__Note__: with this approach you might want to remove two lines above in production.
__Note__: with this approach you might want to remove two lines above in production
By default termlog will __not__ run if it detects production mode using `NODE_ENV`, but you shouldn't rely on that.
By default termlog will __not__ run if it detects production mode using `NODE_ENV`, but you shouldn't rely on that
## I don't want to add dependencies to my project
1. Install the `termlog` binary : `npm install -g termlog`
@ -35,31 +36,37 @@ By default termlog will __not__ run if it detects production mode using `NODE_EN
5. Enter `termlog()` into console
6. You should now see log being streamed to your terminal
__Note__: with this approach you have to do all steps 3-6 every-time you refresh your browser tab.
__Note__: with this approach you have to do all steps 3-6 every-time you refresh your browser tab
## Advanced options
With `tconsole` command:
With `termlog` command:
- `--out path`: Save log to file
- `--port port`: Change server port
- `--addr addr`: Change server address
- `--show levels`: Select log levels to display (info | warning | error | debug). Multiple levels are seperated by `,`
> use `.show levels` while the server running to select again
With `termlog` package:
With `tconsole` package:
`tconsole({
`termlog({
host: "localhost",
port: 3456
})`
## Debug on mobile
To be able to stream log from your app running on mobile you need to:
- Start term log with `0.0.0.0` by running `npx termlog --addr 0.0.0.0`
- Find your private IP address
- MacOS: run `ipconfig getifaddr en0` if you're on wifi and
- Linux: run `hostname -I`
- Windows: run `ipconfig` and find in the printed result. It should be under `192.168.x.x`
- Inside your project init tconfig with: `termlog({host: "YOUR_PRIVATE_IP"})`
## How it works
Tconsole have 2 components:
- [server.js](cli.js) - a websocket server to receive log from browser and display it
- [index.js](index.js) - tconsole package to import in your front end app. This package will override the default behavior of console object and send log to the server
Termlog have 2 components:
- [server.js](cli.js) - a websocket server to receive log message and display on terminal.
- [index.js](index.js) - termlog function to override default behavior of `console` object by capture arguments and send to websocket server
## Future release
- [ ] Install using `<script/>` tag

View File

@ -48,7 +48,7 @@ const termlog = (options = {}) => {
...options,
}
if (!options.disableEnvironmentCheck && process && process.env.NODE_ENV && process.env.NODE_ENV !== 'development') return;
if (!options.disableEnvironmentCheck && typeof process != "undefined" && process.env.NODE_ENV && process.env.NODE_ENV !== 'development') return;
const defaultConsole = Object.assign(Object.create(Object.getPrototypeOf(console)), console);

View File

@ -1,6 +1,6 @@
{
"name": "termlog",
"version": "1.1.1",
"version": "1.1.4",
"description": "Console log to terminal",
"type": "main",
"scripts": {
@ -11,7 +11,7 @@
"url": "git+https://github.com/qnkhuat/termlog.git"
},
"bin": {
"tconsole": "server.js"
"termlog": "server.js"
},
"preferGlobal": true,
"keywords": [],

View File

@ -8,6 +8,7 @@ const DEFAULT_PORT = 3456;
const DEFAULT_PROMPT = "> ";
const CRed = "\x1b[31m";
const CGreen = "\x1b[32m";
const CYellow = "\x1b[33m";
const CWhite = "\x1b[37m";
const CBlue = "\x1b[34m";
@ -30,8 +31,10 @@ const getTime = () => {
const out = (data, color = CWhite) => {
if (!Array.isArray(data)) data = [data];
process.stdout.write(color);
process.stdout.write(CGreen);
process.stdout.write(getTime() + " ");
process.stdout.write(color);
if (typeof data[0] === 'object') {
data[0] = JSON.stringify(data[0], null, 2);