1. Logs in nodeJS
- The log is as follows:
- Access log
access log
, the server
most important log
- Custom logs, including custom events, error records, etc.
nodejs
File operations,nodejs stream
- Development and use of log function
- Log file splitting, log content analysis
- To log to a file, not stored
mysql
and redis
the
- A simple application of the log, the code is as follows:
const http = require('http')
const fs = require('fs')
const path = require('path')
const fileName1 = path.resolve(__dirname, 'data.txt')
const server = http.createServer((req, res) => {
if (req.method === 'GET') {
const readStream = fs.createReadStream(fileName1)
readStream.pipe(res)
}
})
server.listen(8344)
- Log
node JS
application programs, as follows:
log.js
, The code is as follows: const fs = require('fs')
const path = require('path')
function writeLog(writeStram, log) {
writeStram.write(log + '\n')
}
function createWriteStream(fileName) {
const fullFileName = path.join(__dirname, '../', '../', 'logs', fileName)
const writeStream = fs.createWriteStream(fullFileName, {
flags: 'a'
})
return writeStream
}
const accessWriteStream = createWriteStream('access.log')
function access(log) {
writeLog(accessWriteStream, log)
}
module.exports = {
access
}
app.js
, Part of the code is as follows:
access(`${
req.method} -- ${
req.url} -- ${
req.headers['user-agent']} -- ${
Date.now()}`)
IO
The performance bottleneck of the operation is as follows:
IO
, Including network IO
and filesIO
- Compared to the
CPU
computing and memory read and write, IO
the prominent feature is the slow
- Increase under the limited hardware resources
IO
of operational efficiency
- The log split is as follows:
- The content of the log will accumulate slowly, and it is not easy to deal with it in a file
- Divide log files by time, such as
2019-02-10.access.log
- Implementation:
Linux
the crontab
task, that scheduled task
crontab
,As follows:
- Set timed tasks, format:
**** command
- The
access.log
copy and rename2019-02-10.access.log
- Empty
access.log
file, continue to accumulate log
- Log split
node JS
application project, copy.sh
the code is as follows:
#!/bin/sh
cd /node-blog/blog1
cp access.log $(date + %Y-%m-%d).access.log
echo "" > access.log
- The log analysis is as follows:
- As for
access.log
logging, analysis of Chrome
the proportion of
- Logs are stored in rows, one row is one log
- Use
nodejs
of readline
, based on stream
high efficiency
- Log analysis
node JS
application in the project, readline.js
the code is as follows:
const fs = require('fs')
const path = require('path')
const readline = require('readline')
const fileName = path.join(__dirname, '../', '../', 'logs', 'access.log')
const readStream = fs.createReadStream(fileName)
const rl = readline.createInterface({
input: readStream
})
let chromeNum = 0
let sum = 0
rl.on('line', (lineData) => {
if (!lineData) {
return
}
sum++
const arr = lineData.split(' -- ')
if (arr[2] && arr[2].indexOf('Chrome') > 0) {
chromeNum++
}
})
rl.on('close', () => {
console.log('chrome 占比:', + chromeNum / sum)
})