1.0.4 • Published 2 years ago

hachiware_server_module_log v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

hachiware_server_module_log

Log output module of web server package "hachiware_server".

It is a dedicated server module of the web server application "hachiware_server".
To use it, you must first install and configure hachiware_server.

You can install hachiware_server with the following command.

npm i hachiware_server

The server module can be installed with the following npm command.

npm i hachiware_server_module_log

After installation, you will need the hachiware server configuration file. See here for the procedure for using hachiware_server.


# Setting method

Open the configuration file conf/conf.js etc. on the hachiware server and open it. Confirm that hachiware_server_module_log is added in modules.

modules: [
    ...
    "hachiware_server_module_log",
    ...
],

Then specify logs as shown below.
The following is a setting example. Details of the settings will be explained later.

...
logs: {

	// startup write log
	startUp: {
        enable: true,
        path: "logs/startup/startup-{YYYY}.log",
        contents: "[{DATETIME}] {MODE} {HOST}:{PORT} URL= {LISTEN_URI} CONF= {CONF_FILE}",
	},

	// access write log
	access: {
        enable: true,			
        path: "logs/access/access-{YYYY}-{MM}.log",
        contents: "[{DATETIME}] {METHOD} {REQUEST_URL} {REMOTE_IP} {RESPONSE_CODE}",
	},

    // error write log
    error: {
        enable: true,
        path: "logs/error/error-{YYYY}-{MM}.log",
        contents: "[{DATETIME}] {METHOD} {REQUEST_URL} {REMOTE_IP} {RESPONSE_CODE} {ERROR_EXCEPTION} {ERROR_STACK}",
    },
},
...

# Life Cycle

The life cycle of this server module is as follows:

(server listen start)
    |
(request)
    |
load fook access                        <= Output access log.
    |
    ......
        |   * If an error occurs
        |
    load fook error                     <= Output error log.
            |
    error callback action
    ...
    |
    | * Quit the server
    |
(server listen exit)
  • "load fook access" executes the access hook of each sserver module.
  • "load fook error" executes the error hook of the server module.

# Log output when request is received

If you want to output the log when the request is received by the server, specify access in logs.

logs: {
    ...
    // access write log
    access: {
        enable: true,			
        path: "logs/access/access-{YYYY}-{MM}.log",
        contents: "[{DATETIME}] {METHOD} {REQUEST_URL} {REMOTE_IP} {RESPONSE_CODE}",
	},
    ...
},
columoverview
enableSpecify here whether to enable / disable log output when receiving a server request.
pathSpecify the path of the log output destination.If no path is specified, logs/access/access-{YYYY}-{MM}.log is output by default.Specify the path as a relative path.If the directory in the path does not exist, it will be created automatically.Dynamic format placement (DFP) can be used for the path name. Click here for details
contentsSpecify the content to be logged.If no content is specified, it is output as [{DATETIME}] METHOD={METHOD} REQUEST_URI={REQUEST_URL} REMOTE_IP={REMOTE_IP} RESPONSE_CODE={RESPONSE_CODE} by default.You can use Dynamic Format Placement (DFP) for your content Click here for details
callbackCallback to execute when a request is received.Arguments are context, server settings, request object, response object, and set the log output contents in the return value..If no return value is specified, the default content will be output.

# Log output when an error occurs

If you want to output the log when an error occurs in the request from the server, specify error in logs.

logs: {
    ...
    // error write log
    error: {
        enable: true,
        path: "logs/error/error-{YYYY}-{MM}.log",
        contents: "[{DATETIME}] {METHOD} {REQUEST_URL} {REMOTE_IP} {RESPONSE_CODE} {ERROR_EXCEPTION} {ERROR_STACK}",
    },
     ...
},
columoverview
enableSpecify here whether to enable / disable log output when an error occurs after receiving a server request.
pathSpecify the path of the log output destination.If no path is specified, logs/error/error-{YYYY}-{MM}.log is output by default.Specify the path as a relative path.If the directory in the path does not exist, it will be created automatically.Dynamic format placement (DFP) can be used for the path name. Click here for details
contentsSpecify the content to be logged.If no content is specified, it is output as [{DATETIME}] METHOD={METHOD} REQUEST_URI={REQUEST_URL} REMOTE_IP={REMOTE_IP} RESPONSE_CODE={RESPONSE_CODE} ERROR_EXP={ERROR_EXCEPTION} ERROR_STACK={ERROR_STACK} by default.You can use Dynamic Format Placement (DFP) for your content Click here for details
callbackCallback to be executed when an error occurs after receiving a request.The arguments are context, server settings, error information, request object, response object and set the contents of the log output to the return value.If no return value is specified, the default content will be output.

# Dynamic format placement (DFP)

By embedding the error output path and error content as a short code, log information can be dynamically output to any location.

For example, logs can be divided by month, year, etc., making it easier to organize output logs.
There is no need to prepare the batch program required for log collection.

The shortcodes of each format and their output results are as follows.

Short codeaccesserrorOutput result
{DATETIME}Log output date and time.{YYYY}/{MM}/{DD} {HH}:{mm}:{ss}
{DATE}Log output date.{YYYY}/{MM}/{DD}
{TIME}Log output time.{HH}:{mm}:{ss}
{YYYY}Log output year
{MM}Log output month
{DD}Log output day
{HH}Log output hour
{mm}Log output minutes
{ss}Log output second
{HOST}Host name
{PORT}Port Number
{SSL}SSL enabled/disabled
{LISTEN_URI}Requestable URL
{SSNAME}Target server section name
{METHOD}Request method
{REQUEST_URL}Requested URL
{RESPONSE_CODE}Response code number
{REMOTE_IP}Source IP address information
{REQUEST_QUERY}Query information(GET)
{REQUEST_BODY}Request body
{ERROR_EXCEPTION}-Error message
{ERROR_STACK}-Error details

hachiware_server_module_log

Log output module of web server package "hachiware_server".

License : MIT License.
Author : Nakatsuji Masato
HP URL : https://hachiware-js.com/
GitHub : https://github.com/masatonakatsuji2021/hachiware_server_module_log
npm : https://www.npmjs.com/package/hachiware_server_module_log