HLJ 发布于
2022-11-09 19:59:13

nodejs fs.createWriteStream生成日志文件

const http = require('http');
const fs = require('fs');
http.createServer(function (req, res) {

    (async () => {

        const file = fs.createWriteStream('log.txt');
        let logger = new console.Console(file, file);
        logger.log('http://www.good1230.com');
        logger.log('http://good1230.com/tag/1/');
        logger.log('http://good1230.com/tag/2/');

        fs.writeFile('log2.log', '', function (err) {
            if (err) {
                console.log(err);
            }
        });

        let options = {
            flags: 'a', // 
            encoding: 'utf8', // utf8编码
        }
        let stderr = fs.createWriteStream('log2.log', options);
        let logger2 = new console.Console(stderr);
        logger2.log('http://www.good1230.com');
        logger2.log('http://good1230.com/tag/1/');
        logger2.log('http://good1230.com/tag/2/');
        logger2.log('http://good1230.com/tag/3/');
        logger2.log('http://good1230.com/tag/4/');
        logger2.log('http://good1230.com/tag/5/');
        logger2.log('http://good1230.com/tag/6/');

        // 定时清空内容
        setTimeout(() => {
            fs.writeFile('log2.log', '', function (err) {
                if (err) {
                    console.log(err);
                }
            });
        }, 1000 * 3600 * 24); // 每24小时清空

    })();

    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.write('Node.js says hello!');
    res.end();
}).listen(8080);
当前文章内容为原创转载请注明出处:http://www.good1230.com/detail/2022-11-09/616.html
最后生成于 2022-11-21 22:22:39