Skip to content

Commit 392ba87

Browse files
committed
added logTime to exclude the time of access
set default parameter of log to '' (nothing)
1 parent 854de8e commit 392ba87

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ Default:
3838
}
3939
```
4040
### `log`
41-
Create a log file with the given name (default is `off`) (*optional*)
41+
Create a log file with the given name (default is ``, which means no logging) (*optional*)
42+
43+
### `logTime`
44+
Include the time of access in the log file (default is `true`) (*optional*)
4245

4346
## Example
4447
```yaml
@@ -73,6 +76,7 @@ steps:
7376
"xml": "text/xml"
7477
}
7578
log: "log.txt"
79+
logTime: "false"
7680
-
7781
run: |
7882
curl -vvvv http://localhost:8080/index.html

action.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ inputs:
4949
log:
5050
description: 'Create a log file with given name'
5151
required: false
52-
default: 'off'
52+
default: ''
53+
logTime:
54+
description: 'Whether to include the time of access in the log file or not'
55+
required: false
56+
default: true
5357
runs:
5458
using: 'node16'
5559
main: 'main.js'

main.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ let config = {
3333
indexFiles: null,
3434
allowedMethods: null,
3535
contentTypes: null,
36-
log: null
36+
log: null,
37+
logTime: null
3738
};
3839

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

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

100+
config.logTime = core.getInput('logTime');
101+
if (config.logTime === null || config.logTime.length == 0) {
102+
config.logTime = true;
103+
} else {
104+
config.logTime = config.logTime === 'true';
105+
}
106+
99107
const cp = require('child_process');
100108
const child = cp.fork(__filename, ['serve'], { detached: true, silent: true });
101109
child.on('error', (err) => {

server.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ function deploy(config, ready) {
2626
config.contentTypes = {};
2727
}
2828
if (config.log == undefined || config.log == null) {
29-
config.log = "off";
29+
config.log = "";
30+
}
31+
if (config.logTime == undefined || config.logTime == null) {
32+
config.logTime = true;
3033
}
3134

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

4245
let writeLine = (line) => {
43-
if (config.log !== "off") {
44-
let txtLogger = fs.createWriteStream(config.log, {
45-
flags: 'a'
46-
});
47-
48-
txtLogger.write(`\n${line}`);
49-
}
46+
let txtLogger = fs.createWriteStream(config.log, {
47+
flags: 'a'
48+
});
49+
50+
txtLogger.write(`\n${line}`);
5051
};
5152

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

56-
request.on('data', (chunk) => {
57-
data += chunk;
58-
});
59-
60-
request.on('end', () => {
61-
writeLine(`[${now}] ${request.method} ${request.url} ${JSON.stringify(data)}`);
62-
});
58+
request.on('data', (chunk) => {
59+
data += chunk;
60+
});
6361

62+
request.on('end', () => {
63+
writeLine(`${now}${request.method} ${request.url} ${JSON.stringify(data)}`);
64+
});
65+
}
6466

6567
if (config.noCache) {
6668
response.setHeader(

0 commit comments

Comments
 (0)