diff --git a/README.md b/README.md index e1ed241..6749bba 100644 --- a/README.md +++ b/README.md @@ -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 @@ -73,6 +76,7 @@ steps: "xml": "text/xml" } log: "log.txt" + logTime: "false" - run: | curl -vvvv http://localhost:8080/index.html diff --git a/action.yml b/action.yml index 6f17d5f..4e911e3 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/main.js b/main.js index fce243f..a36ffa5 100644 --- a/main.js +++ b/main.js @@ -33,7 +33,8 @@ let config = { indexFiles: null, allowedMethods: null, contentTypes: null, - log: null + log: null, + logTime: null }; config.root = core.getInput('directory'); @@ -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) => { diff --git a/server.js b/server.js index 821aa7f..5a4ab62 100644 --- a/server.js +++ b/server.js @@ -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)); @@ -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(