Installing the Node Environment

Since this framework is based on Node.js, installing Node is a necessary first step, which I won’t cover in detail here

  • The current environment

Install KOA (we use Taobao image for installation)

CNPM install koa-generator -g # Create new project koa2 my_server # Open new project CD my_server # Install dependent on YARNCopy the code

After the installation is complete, run NPM dev and enter localhost:3000. The following page is displayed, indicating that the KOA2 environment is successfully set up

  • The console

  • The browser

The entry file is in bin/ WWW

Install the LogJS plug-in

NPM log4JS document address

Install log4js

NPM install log4js # or yarn add log4jsCopy the code

Configuration log4js

Create a utils folder and create log4j.js

Comments are all on the code, any questions are welcome to harass me

/** * Log storage * @auther He young life. * @time 2021/07/28 22:11 */ const log4j = require('log4js') const levels = { 'trace' : log4j.levels.TRACE, 'debug' : log4j.levels.DEBUG, 'info' : log4j.levels.INFO, 'warn' : log4j.levels.WARN, 'error' : log4j.levels.ERROR, 'fatal' : Log4j. configure({appenders: {console: {type: 'console'}, info: {type: 'console'}) 'file', filename: 'logs/all-logs.log' }, error: { type: 'dateFile', filename: 'logs/log', pattern: 'yyyy-mm-dd. log', alwaysIncludePattern: true // set filename to filename + pattern}}, categories: {default: {appenders: [ 'console' ], level: 'debug' }, info: { appenders: [ 'info', 'console' ], level: 'info' }, error: { appenders: [ 'error', 'console' ], level: 'error'}}}) /** * Output level is bug * @param {string} content */ export.debug = (content) => {let logger = Log4j.getlogger ('debug') logger.level = levels.debug logger.debug(content)} /** * Log output level info * @param {string} content */ exports.info = ( content ) => { let logger = log4j.getLogger('info') logger.level = levels.info Logger.info (content)} /** * The log output level is error * @param {string} content */ export.error = (content) => {let logger  = log4j.getLogger('error') logger.level = levels.error logger.error(content) }Copy the code

defineutil.jsFile, do general utility function library

/** * General function encapsulation * @Auther He young life. * @time 2021/07/28 22:07 */ const log4j = require('./log4j') const CODE = {SUCCESS: 200, // successful PARAM_ERROR: USER_LOGIN_ERROR: 30001, // The user does not log in to BUSINESS_ERROR: 40001, // Service request failed AUTH_ERROR: In 50001, } module. Exports = {@param {number} pageNum * @param {number} pageSize Page number */ pager({ pageNum = 1, PageSize = 1}) {pageNum *= 1 const skipIndex = (pageSize - 1) * pageSize = 1 const skipIndex = (pageSize - 1) {pageNum, pageSize}, skipIndex}}, (data = ', MSG = ', Code = code.success) {log4J.debug (data) return {code, data, MSG}}, // Fail callback (data = ", MSG = ", Code = code >BUSINESS_ERROR) {log4j.debug(MSG) return {code, data, MSG}}, //Copy the code

At this point, the encapsulation of Log4J is complete and you can call it directly

Add two sentences to app.js to print get and POST requests in real time

// Logger middleware app.use(async (CTX, next) => {log4j.info(' get: ${json.stringify (ctX.request.query)} ') // Listen to get request log4j.info(' params: ${json.stringify (ctX.Request.body)} ') // Listen to post request const start = new Date() await next() const ms = new Date() -start console.log(`${ctx.method} ${ctx.url} - ${ms}ms`) })Copy the code

Then run NPM dev, open localhost: 3000 in your browser, and you will see that the project automatically generates a log folder containing the log information

Finally:

Public number: xiao He growth, The Buddha department more text, are their own once stepped on the pit or is learned

Interested partners welcome to pay attention to me, I am: He young life. Everybody progress duck together