Skip to content
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

Improve Logging #27

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ Default:
}
```
### `log`
Create a log file with the given name (default is `off`) (*optional*)
Create a log file with the given name (default is ``, which means no logging) (*optional*)

### `logTime`
Include the time of access in the log file (default is `true`) (*optional*)

## Example
```yaml
Expand Down Expand Up @@ -73,6 +76,7 @@ steps:
"xml": "text/xml"
}
log: "log.txt"
logTime: "false"
-
run: |
curl -vvvv http://localhost:8080/index.html
Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ inputs:
log:
description: 'Create a log file with given name'
required: false
default: 'off'
default: ''
logTime:
description: 'Whether to include the time of access in the log file or not'
required: false
default: true
runs:
using: 'node16'
main: 'main.js'
Expand Down
10 changes: 9 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ let config = {
indexFiles: null,
allowedMethods: null,
contentTypes: null,
log: null
log: null,
logTime: null
};

config.root = core.getInput('directory');
Expand Down Expand Up @@ -96,6 +97,13 @@ if (config.allowedMethods === null || config.allowedMethods.length == 0) {

config.log = core.getInput("log");

config.logTime = core.getInput('logTime');
if (config.logTime === null || config.logTime.length == 0) {
config.logTime = true;
} else {
config.logTime = config.logTime === 'true';
}

const cp = require('child_process');
const child = cp.fork(__filename, ['serve'], { detached: true, silent: true });
child.on('error', (err) => {
Expand Down
38 changes: 20 additions & 18 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ function deploy(config, ready) {
config.contentTypes = {};
}
if (config.log == undefined || config.log == null) {
config.log = "off";
config.log = "";
}
if (config.logTime == undefined || config.logTime == null) {
config.logTime = true;
}

const root = path.resolve(path.normalize(config.root));
Expand All @@ -40,27 +43,26 @@ function deploy(config, ready) {
}

let writeLine = (line) => {
if (config.log !== "off") {
let txtLogger = fs.createWriteStream(config.log, {
flags: 'a'
});

txtLogger.write(`\n${line}`);
}
let txtLogger = fs.createWriteStream(config.log, {
flags: 'a'
});

txtLogger.write(`\n${line}`);
};

server.on('request', (request, response) => {
let now = formatTime.format(new Date());
let data = '';
server.on('request', (request, response) => {
if (config.log !== "") {
let now = config.logTime ? `[${formatTime.format(new Date())}] ` : '';
let data = '';

request.on('data', (chunk) => {
data += chunk;
});

request.on('end', () => {
writeLine(`[${now}] ${request.method} ${request.url} ${JSON.stringify(data)}`);
});
request.on('data', (chunk) => {
data += chunk;
});

request.on('end', () => {
writeLine(`${now}${request.method} ${request.url} ${JSON.stringify(data)}`);
});
}

if (config.noCache) {
response.setHeader(
Expand Down