Skip to content

Commit

Permalink
added logTime to exclude the time of access
Browse files Browse the repository at this point in the history
set default parameter of log to '' (nothing)
  • Loading branch information
tts-sdrissen committed Jul 14, 2023
1 parent 854de8e commit 392ba87
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
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

0 comments on commit 392ba87

Please sign in to comment.