nodejs 关于log4js输出到指定文件中

      感觉nodejs关于日志(log4js)管理网上有N个版本,下面log4js配置是我经过运行成功的案例!!!

第一:

下载安装log4js包(项目下输入路径这个命令)

npm install log4js

 

 安装说明(win7 cmd命令):

进入自己项目的盘符

执行安装命令:npm install log4js

编写app.js  我的是写在引入模块下面(全部的引入模块下)我的nodejs版本:v6.11.2  运行环境:win7

        filename:'./logs/:日志输出目录(项目根目录下)

//添加日志管理开始
const log4js = require('log4js');
log4js.configure({
    appenders: {
        xcLogFile: {
            type: "dateFile",
            filename: __dirname +'/logs/LogFile',//
            alwaysIncludePattern: true,
            pattern: "-yyyy-MM-dd.log",
            encoding: 'utf-8',//default "utf-8",文件的编码
            maxLogSize: 11024 }, //文件最大存储空间
        xcLogConsole: {
            type: 'console'
        }
    },
    categories: {
        default: {
            appenders: ['xcLogFile'],
            level: 'all'
        },
        xcLogFile: {
            appenders: ['xcLogFile'],
            level: 'all'
        },
        xcLogConsole: {
            appenders: ['xcLogConsole'],
            level: log4js.levels.ALL
        }
    }
});
module.exports = log4js.getLogger('xcLogConsole');
var logger = log4js.getLogger('log_file');
//添加日志管理结束

第二:日志输出规则:

logger.info("发报格式错,报文发送失败");
 
输出的LogFile-2017-08-23.log的日志:
 
[2017-08-23 16:54:47.126] [INFO] log_file - SERVER START
[2017-08-23 16:54:52.535] [INFO] log_file - AddServer START
[2017-08-23 16:54:52.785] [INFO] log_file - AddServer END
[2017-08-23 16:54:53.827] [INFO] log_file - 往报发送时遇到错误【Error: connect ECONNREFUSED】,失败!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 `child_process` 模块的 `execFile` 函数来运行指定路径的 JavaScript 文件。 例如,假设你有一个名为 `script.js` 的文件,你可以这样使用 `execFile` 函数来运行它: ``` const { execFile } = require('child_process'); const scriptPath = '/path/to/script.js'; execFile(scriptPath, (error, stdout, stderr) => { if (error) { console.error(error); return; } console.log(stdout); }); ``` 注意,`execFile` 函数是异步的,因此它会立即返回,而不会等待脚本执行完成。如果你需要在脚本执行完成后执行某些操作,你可以在回调函数执行这些操作。 需要注意的是,如果你希望脚本运行独立于原来的命令窗口,你需要使用 `child_process` 模块的 `spawn` 函数,而不是 `execFile` 函数。 例如,你可以这样使用 `spawn` 函数来运行指定路径的 JavaScript 文件: ``` const { spawn } = require('child_process'); const scriptPath = '/path/to/script.js'; const child = spawn('node', [scriptPath]); ``` 这样,脚本就会在一个新的进程运行,独立于原来的命令窗口。 ### 回答2: 在Node.js,可以使用`execFile`函数来运行指定路径的JavaScript文件并且独立于原来的命令窗口。 `execFile`函数是`child_process`模块的一个方法,用于在子进程执行可执行文件。要使用`execFile`函数运行JS文件,首先需要在代码引入`child_process`模块。 ```javascript const { execFile } = require('child_process'); ``` 然后,使用`execFile`函数来执行指定路径的JS文件。 ```javascript const child = execFile('node', ['path/to/js/file.js'], (error, stdout, stderr) => { if (error) { throw error; } console.log(stdout); // 子进程输出的内容 }); ``` 在`execFile`函数,第一个参数是要执行的可执行文件,在这里是Node.js的`node`命令。第二个参数是一个数组,包含要运行的JS文件的路径。回调函数的`stdout`参数是子进程输出的内容,可以用来处理子进程的输出。 在使用`execFile`函数运行JS文件时,子进程是独立的,与原来的命令窗口无关。也就是说,在运行JS文件时,不会打开一个新的命令窗口,而是在当前命令窗口并行执行指定路径的JS文件。 ### 回答3: 在Node.js,可以使用`execFile`函数来运行指定路径的JavaScript文件,并且独立于原来的命令窗口。 `execFile`是`child_process`模块的一个函数,它用于执行外部的Shell命令或可执行文件。将其与指定路径的JavaScript文件结合使用,可以实现运行独立于原来的命令窗口的功能。 下面是一个示例代码,演示了如何使用`execFile`函数来运行指定路径的JavaScript文件,并独立于原来的命令窗口: ```javascript const { execFile } = require('child_process'); // 指定JavaScript文件的路径 const filePath = 'path/to/your/js/file.js'; // 执行指定路径的JavaScript文件 execFile('node', [filePath], (error, stdout, stderr) => { if (error) { console.error(error); return; } console.log('执行结果:', stdout); }); ``` 在代码,首先引入了`child_process`模块,并使用`execFile`函数来执行指定路径的JavaScript文件。其,第一个参数为`node`命令,表示要运行的文件是一个JavaScript文件;第二个参数为一个数组,其包含了要执行的JavaScript文件的路径;第三个参数是一个回调函数,用于处理执行结果。 执行结果将通过回调函数的`stdout`参数输出。如果发生错误,可以在回调函数的`error`参数进行处理,在这里我们简单地将错误输出到控制台。 通过使用`execFile`函数,指定路径的JavaScript文件将以独立于原来的命令窗口的方式运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值