Git Product home page Git Product logo

Comments (2)

xeioex avatar xeioex commented on August 20, 2024

Hi @baiyuxiong,

There is no way to separate njs logs and nginx error logs as of now.

One way to separate them is to filter the error.log file as
grep '\[info\].*js' error.log
to get NJS logs.

from njs.

stefanwerfling avatar stefanwerfling commented on August 20, 2024

When it comes to logging your own logs separately or per server/location, there is a trick to logging.

First you create your own SysLog server with JS, which waits for messages, and you parse them into JSON. You then evaluate this based on an ID, you discard everything else, or you log β€œBasic”. You can split everything with an ID (by me "ff_http_id":3) separately into files or into a DB, as you like:


public static readonly SYSLOG_TAG = 'nginx';

......

syslog = new SysLogServer();
....
syslog.setOnMessage((
            _sysLogServer,
            msg
        ) => {
            
            const parts = msg.toString().split(`${NginxService.SYSLOG_TAG}: `);

            try {
                const nginxLog = JSON.parse(parts[1]);
....

In the nginx config you log a log_format:

log_format ff_h_accesslogs_3 escape=json '{"source":"nginx","source_type":"http","logging":"access","ff_http_id":3,"time":"$time_iso8601","host":"$remote_addr","proxy_protocol_addr":"$proxy_protocol_addr","forwardedfor":"$http_x_forwarded_for","req":"$request","method":"$request_method","scheme":"$scheme","uri":"$request_uri","status":"$status","size":"$body_bytes_sent","referer":"$http_referer","ua":"$http_user_agent","reqtime":"$request_time","runtime":"$upstream_http_x_runtime","apptime":"$upstream_response_time","cache":"$upstream_http_x_cache","vhost":"$host"}';

then:

	server {
		listen 10443 ssl http2 proxy_protocol;
		access_log syslog:server=127.0.0.1:514,tag=nginx ff_h_accesslogs_3;

In njs javascript you can now put together such a JSON string and log it:

async function some(s: NginxHTTPRequest) {
        s.warn('{"source":"njs", "ff_http_id":3, ......}');

Maybe this approach will help you :)

from njs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.