WebReference.com - Chapter 4 from The mod_perl Developer's Cookbook, from Sams Publishing (4/6) | WebReference

WebReference.com - Chapter 4 from The mod_perl Developer's Cookbook, from Sams Publishing (4/6)

To page 1To page 2To page 3current pageTo page 5To page 6
[previous] [next]

mod_perl Developer's Cookbook

4.6. Controlling the LogLevel

You want to be able to determine the current LogLevel setting.

Technique

Use the loglevel() method from the Apache::Server class.

use Apache::Log;
use strict;
sub handler {
 my $r = shift;
 my $loglevel = $r->server->loglevel;
 unless ($r->content_type eq 'text/html') {
  $r->log->info("Request is not for an html document - skipping...")
   if $loglevel >= Apache::Log::INFO;
  return DECLINED;
 }
 # Continue along...
}

Comments

We mentioned in Recipe 4.4 that mod_perl may one day optimize calls to the Apache::Log class. This would mean that code brought in by a PerlModule directive would be optimized at compile time to nullify calls to, say, $r->log->warn() if the current LogLevel setting would render the call meaningless. Although mod_perl has not yet reached the point where behind-the-scenes optimizations like this are built-in, with the loglevel() directive we can be somewhat more intelligent about logging and eke out some additional performance gains.

Although the loglevel() method is called from an Apache::Server object, the implementation is such that you must use Apache::Log; in order to gain access to the method, as shown in the sample code.

The Apache::Log class provides eight constants that correspond to the eight different LogLevel settings:

Keep in mind that Apache::Log::EMERG is currently lower in numerical value than Apache::Log::DEBUG, so be sure to code accordingly.

One additional point of interest is that loglevel() is writeable, making code like this possible in a PerlCleanupHandler:

package Cookbook::TraceError;
use Apache::Constants qw(OK SERVER_ERROR DECLINED);
use Apache::Log;
use strict;
sub handler {
 # Enable all debugging stuff and re-run (most of) the request.
 # Use in a PerlCleanupHandler.
 my $r = shift;
 # Don't do anything unless the main process errors.
 return DECLINED unless $r->is_initial_req &&
             $r->status == SERVER_ERROR;
 # Get the old LogLevel while setting the new value
 my $old_loglevel = $r->server->loglevel(Apache::Log::DEBUG);
 # Set some other trace routines.
 my $old_trace = DBI->trace(2);
 # Start the debuggging request.
 my $sub = $r->lookup_uri($r->uri);
 # run() would ordinarily send content to the client, but
 # since we're in cleanup, the connection is already closed.
 $sub->run;
 # Reset things back to their original state -
 # loglevel(N) will persist for the lifetime of the child process.
 DBI->trace($old_trace);
 $r->server->loglevel($old_loglevel);
 return OK;
}
1;

4.7. Accessing the Apache::Connection Object

You want to find out details about the current connection.

Technique

Create an Apache::Connection object and use its class methods.

use Socket ();
use strict;
sub handler {
 my $r = shift;
 my $c = $r->connection;
 my $ip_address = $c->remote_ip;
 my $remote_host = $c->remote_host;
 my $local_addr = $c->local_addr;
 my $remote_addr = $c->remote_addr;
 # Call Socket methods on local/remote address.
 my ($localport, $local_ip) = Socket::sockaddr_in($local_addr);
 # Make the IP address human-readable.
 $local_ip = Socket::inet_ntoa($local_ip);
 # Continue along...
}

Comments

The Apache::Connection class offers access to information from the Apache connection record. Similar to the Apache::Server class, creation of an Apache::Connection object is done through the request object via the connection() method. Unlike the Apache::Server object, however, an Apache::Connection object is only available at request time, because the object represents an open connection between the server and client. Quite a bit of information is available in this class, including IP addresses, hostnames, local and remote socket endpoints, and even IDENT style username lookups. Traditionally, the Apache::Connection object is placed into the $c variable.

Calling remote_ip() always returns the IP address for the remote client. The remote_host() method is available for trying to obtain the hostname, but is deprecated in favor of the get_remote_host() method described in the next recipe; this is because get_remote_host() caches the results of lookups, and also returns the IP address if the DNS lookup wasn't successful.

In addition, you can access the packed addresses for the local and remote socket endpoints through, respectively, local_addr() and remote_addr(). As the sample code at the beginning of this recipe illustrates, this allows you to find the IP addresses and ports for the local endpoint and the remote endpoint by using the Socket::sockaddr_in() and Socket::inet_ntoa() functions.

Although not terribly useful these days because most shops summarily disable identd, the Apache::Connection object also provides the remote_logname() method. This gives the name of the remote user if the IdentityCheck configuration option is set to On and the client machine is running the identd daemon. Note that this option will slow down your connections and may not provide accurate information (if it provides any at all). Its use is deprecated in favor of the get_remote_logname() method, which caches the results.

The full list of Apache::Connection class methods is given in Table 4.2.

Table 4.2 Some Apache::Connection Methods

Method

Example Value

Details

aborted()

TRUE

Returns true if the connection to the client is lost.

auth_type()

Basic

Gets or sets the authentication scheme actually used for the request, typically either Basic or Digest.

fileno()

file_descriptor

Returns the file descriptor for the output filehandle, or the input file descriptor if explicitly passed 0.

local_addr()

packed address

Returns the server IP address and port in packed format.

remote_addr()

packed address

Gets or sets the client IP address and port in packed format.

remote_host()

minnow.example.com

Returns the hostname of the client, if available.

remote_ip()

10.3.4.200

Gets or sets the IP of the client.

remote_logname()

Ginger

Returns the username as provided by identd, if available. Deprecated in favor of $r->get_remote_logname().

user()

Professor

Gets or sets the authenticated user Deprecated in favor of $r->user().



To page 1To page 2To page 3current pageTo page 5To page 6
[previous] [next]

Copyright © Pearson Education and
Created: March 18, 2002
Revised: March 18, 2002


URL: http://webreference.com/programming/perl/cookbook/chap4/4.html