Skip to content

Latest commit

 

History

History
148 lines (106 loc) · 4.55 KB

API.md

File metadata and controls

148 lines (106 loc) · 4.55 KB

API

Contents

To get started, first you'll need to create a HttpLogger instance. Here there are options to specify a URL (for where JSON messages will be sent) and/or a specific set of logging rules (for what privacy protections to apply). Default values will be used for either of these if specific values are not provided.

import io.resurface.*;

// with default url and rules
HttpLogger logger = new HttpLogger();

// with specific url and default rules
logger = new HttpLogger("https://...");

// with specific url and rules
logger = new HttpLogger("https://...", "include strict");

// with specific url and rules from local file
logger = new HttpLogger("https://...", "file://./rules.txt");

Now that you have a logger instance, let's do some logging. Here you can pass standard request/response objects, as well as response body and request body content when these are available.

// with standard objects
HttpMessage.send(logger, request, response);

// with response body
HttpMessage.send(logger, request, response, "my-response-body");

// with response and request body
HttpMessage.send(logger, request, response, "my-response-body", "my-request-body");

If standard request and response objects aren't available in your case, create mock implementations to pass instead.

// define request to log
HttpServletRequest request = new HttpServletRequestImpl();
request.setCharacterEncoding("UTF-8");
request.setContentType("application/json");
request.setHeader("A", "123");
request.setMethod("POST");
request.setParam("B", "234");   // POST param
request.setRequestURL("http://resurface.io");

// define response to log
HttpServletResponse response = new HttpServletResponseImpl();
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
response.setHeader("B", "234");
response.setStatus(200);

// log objects defined above
HttpMessage.send(logger, request, response);

If no rules are provided when creating a logger, the default value of include strict will be applied. A different default value can be specified as shown below.

HttpRules.setDefaultRules("include debug");

When specifying multiple default rules, put each on a separate line.

HttpRules.setDefaultRules(
    "include debug\n" +
    "sample 10\n"
);

If your application creates more than one logger, or requires different URLs for different environments (development vs testing vs production), then set the USAGE_LOGGERS_URL environment variable. This value will be applied if no other URL is specified when creating a logger.

# when launching Java app
java -DUSAGE_LOGGERS_URL="https://..." ...

# from command line
export USAGE_LOGGERS_URL="https://..."

# for Heroku app
heroku config:set USAGE_LOGGERS_URL=https://...

Individual loggers can be controlled through their enable and disable methods. When disabled, loggers will not send any logging data, and the result returned by the log method will always be true (success).

All loggers for an application can be enabled or disabled at once with the UsageLoggers class. This even controls loggers that have not yet been created by the application.

UsageLoggers.disable();    // disable all loggers
UsageLoggers.enable();     // enable all loggers

All loggers can be permanently disabled with the USAGE_LOGGERS_DISABLE environment variable. When set to true, loggers will never become enabled, even if UsageLoggers.enable() is called by the application. This is primarily done by automated tests to disable all logging even if other control logic exists.

# when launching Java app
java -DUSAGE_LOGGERS_DISABLE="true" ...

# from command line
export USAGE_LOGGERS_DISABLE="true"

# for Heroku app
heroku config:set USAGE_LOGGERS_DISABLE=true

© 2016-2024 Graylog, Inc.