mod_servlet
C++Servlets
 All Classes Files Functions Variables Typedefs Macros Pages
servlet::logging::log_registry Class Reference

Registry object to configure and hold available loggers. More...

#include <logger.h>

Public Types

typedef std::map< std::string,
std::string, std::less<> > 
properties_type
 Type definition for properties map.
 

Public Member Functions

 log_registry ()
 Creates registry object with default settings.
 
std::shared_ptr< loggerlog (const std::string &name="root")
 Searches for registered logger with a given name. More...
 
void read_configuration (const std::string &config_file_name, const std::string &base_dir, bool update_loggers=true)
 Reads configuration file and parses properties as described in class documentation. More...
 
void read_configuration (properties_type props, const std::string &base_dir, bool update_loggers=true)
 Reads configuration properties from provided properies map. More...
 
LEVEL get_log_level (const std::string &name)
 Returns logging level for a logger with a given name. More...
 
void set_log_level (LEVEL level)
 Sets new global logging level. More...
 
void imbue (const std::locale &loc)
 Imbue locale. More...
 
void set_base_directory (const std::string &base_dir)
 Sets base directory. More...
 
void set_base_directory (std::string &&base_dir)
 Sets base directory. More...
 
SYNC_POLICY get_synchronization_policy () const
 Returns current synchronization policy used by this registry. More...
 
void set_synchronization_policy (SYNC_POLICY sync_policy)
 Sets new synchronization policy. More...
 
void set_prefix_printer (prefix_printer *pp)
 Sets new prefix_printer for this registry. More...
 
void set_log_output (log_output *log_out)
 Sets new log_output for this registry. More...
 
void reset_loggers_config (bool update_prefix_printer, bool update_log_output, bool update_locale)
 Traverses all created loggers and resets their configuration with new configuration parameters. More...
 

Static Public Attributes

static constexpr std::size_t DEFAULT_FILE_ROTATION_SIZE = 1024*1024*16
 Default file rotation size - 16 Mb.
 
static constexpr std::size_t DEFAULT_ASYNC_QUEUE_SIZE = 1024
 Default size of asynchronous queue used if synchronization policy is SYNC_POLICY::ASYNC
 

Protected Member Functions

void add_prefix_printer_factory (const std::string &name, prefix_printer_factory *fac)
 This method allows to provide custom prefix_printer's in inherited registries. More...
 
void add_log_output_factory (const std::string &name, log_output_factory *fac)
 This method allows to provide custom log_output's in inherited registries. More...
 

Detailed Description

Registry object to configure and hold available loggers.

This object generates new logger objects when requested and configures them as prescribed by the configuration of this registry object. The configuration can be done either programatically (via methods set_log_level, imbue, set_base_directory, set_synchronization_policy, set_prefix_printer, set_log_output), or by properties file (methods read_configuration). This object supports the following configuration parameters:

  • output.handler - specifies output handler to be used. It supports the following ahdler types:
    1. console - redirects logging output to std::cout
    2. file - redirects logging output to file. Uses the following configuration parameters:
      1. file.log.file - name of the logging file. It can be specified with either absolute path or relative, which can be resolved from base_dir Name of the file also can use date and index elements. For example the pattern. "log-%y-%m-%d.%NNN%.out" will resove to "log-16-22-11.001.out"
        1. "%y" - resolves to last two digits of a current year;
        2. "%Y" - resolves to four digits of a current year;
        3. "%m" - resolves to two digits of a current month;
        4. "%d" - resolves to two digits of a current day;
        5. "%NN%" - resolves to the number of this file padded to number of digits equals to number of 'N' caracters in the pattern. This element can be used only if size rotation for the file is requested (see size-file and date-size-file;
    3. size-file - redirects logging output to file and uses rotation by size. Plus to the log.file configuration parameters with "size-file" prefix also uses following:
      1. size-file.rotation size - maximum size of the file after which this file will be closed and new one opened with different index. Default rotation size is 16 Mb.
    4. date-file - redirects logging output to file and uses rotation by date (00:00:00 of each day). Requires log.file property with "date-file" prefix.
    5. date-size-file - redirects logging output to file and uses rotation by both date and size.
    Default output.handler is console
  • prefix.printer - specifies prefix printer to be used. Currently the following printer types are supported:
    1. simple - simple prefix printer which prints the line prefix accordingly to the following parameters:
      1. simple.log.prefix.format - string to be in the prefix. For example: "[%TIME%] [%NAME%] [%LEVEL%]: ". The follwing pattern elements can be used in the format:
        1. %TIME% - current timestamp (as prescribed by "timestamp.format" parameter);
        2. %THREAD% - current thread id;
        3. %LEVEL% - current logging level;
        4. %NAME% - logger name;
        The default format is "%TIME% | %THREAD% | %LEVEL% | %NAME% | "
      2. simple.timestamp.format - format of the timestamp. It follows the specifications of std::strftime function with the addition of %ss specifier which will be substituted with milliseconds. The default timestamp format is: "%y/%m/%d %H:%M:%S.%ss". It is highly recommended to use default format as it performs mych better than custom formats.
  • sync.policy - logging synchronization policy. It can be one of: SINGLE_THREAD, SYNC or ASYNC. For more informations see SYNC_POLICY. Default value is SYNC
  • .level - root logging level. This is a global logging level, which will be applied to all loggers which dont have logger specific configuration. Default logging level is WARNING
  • logger-name.level - logging level for logger with name "logger-name".
  • locale - valid locale to be used to print locale specific elements. For example "uk_UA.utf8"

Configuration can be updated runtime and provided to the registry. All parameters (except sync.policy which cannot be changed runtime) will be updated.

Member Function Documentation

void servlet::logging::log_registry::add_log_output_factory ( const std::string &  name,
log_output_factory fac 
)
inlineprotected

This method allows to provide custom log_output's in inherited registries.

For example if there is a custom system_log_output it can be added in inherited class as follows:

class my_log_registry
{
my_log_registry()
{
add_log_output_factory("sys", new system_log_output_factory{});
}
};

And after this it can read configuration with prefix "sys."

Parameters
nameNew log_output_factory name.
faclog_output factory.
void servlet::logging::log_registry::add_prefix_printer_factory ( const std::string &  name,
prefix_printer_factory fac 
)
inlineprotected

This method allows to provide custom prefix_printer's in inherited registries.

For example if there is a custom color_prefix_printer it can be added in inherited class as follows:

class my_log_registry
{
my_log_registry()
{
add_prefix_printer_factory("color", new color_prefix_printer_factory{});
}
};

And after this it can read configuration with prefix "color."

Parameters
nameNew prefix_printer_factory name.
facprefix_printer factory.
LEVEL servlet::logging::log_registry::get_log_level ( const std::string &  name)

Returns logging level for a logger with a given name.

If no logger present, global logging level will be returned.

Parameters
namelogger name
Returns
loggin glevel of requested logger.
SYNC_POLICY servlet::logging::log_registry::get_synchronization_policy ( ) const
inline

Returns current synchronization policy used by this registry.

Returns
this registry synchronization policy
void servlet::logging::log_registry::imbue ( const std::locale &  loc)
inline

Imbue locale.

Associates loc to this registry as the new locale object to be used with locale-sensitive operations. All new loggers created by this registry will receive this locale.

Parameters
locNew locale for the registry.
std::shared_ptr<logger> servlet::logging::log_registry::log ( const std::string &  name = "root")

Searches for registered logger with a given name.

If not found new logger with this name will be created, registered with this registry and returned.

Parameters
nameLogger name.
Returns
Registered instance of a logger with a given name.
void servlet::logging::log_registry::read_configuration ( const std::string &  config_file_name,
const std::string &  base_dir,
bool  update_loggers = true 
)

Reads configuration file and parses properties as described in class documentation.

Parameters
config_file_nameName of configuration file.
base_dirBase directory to use to resolve relative paths.
update_loggersIf true all registered loggers will be updated with new configuration parameters.
void servlet::logging::log_registry::read_configuration ( properties_type  props,
const std::string &  base_dir,
bool  update_loggers = true 
)

Reads configuration properties from provided properies map.

Properties are described in class documentation.

Parameters
propsProperties map.
base_dirBase directory to use to resolve relative paths.
update_loggersIf true all registered loggers will be updated with new configuration parameters.
void servlet::logging::log_registry::reset_loggers_config ( bool  update_prefix_printer,
bool  update_log_output,
bool  update_locale 
)

Traverses all created loggers and resets their configuration with new configuration parameters.

Parameters
update_prefix_printerif true updates prefix_printer for loggers.
update_log_outputif true updates log_output for loggers.
update_localeif true updates locales for loggers.
void servlet::logging::log_registry::set_base_directory ( const std::string &  base_dir)

Sets base directory.

This directory is used to resolve any relative path parameters in the configuration.

Parameters
base_dirNew base directory.
void servlet::logging::log_registry::set_base_directory ( std::string &&  base_dir)

Sets base directory.

This directory is used to resolve any relative path parameters in the configuration.

Parameters
base_dirNew base directory.
void servlet::logging::log_registry::set_log_level ( LEVEL  level)
inline

Sets new global logging level.

Parameters
levelLevel to set.
void servlet::logging::log_registry::set_log_output ( log_output log_out)

Sets new log_output for this registry.

Parameters
log_outNew log_output to be used with this registry.
void servlet::logging::log_registry::set_prefix_printer ( prefix_printer pp)
inline

Sets new prefix_printer for this registry.

Parameters
ppnew prefix_printer to be used by this logger
void servlet::logging::log_registry::set_synchronization_policy ( SYNC_POLICY  sync_policy)

Sets new synchronization policy.

This method will have any effect only if this registry is not yet configured and no loggers are created.

Parameters
sync_policynew synchronization polisy to set.
Exceptions
configu_exceptionis thrown if loggers has already been created.

The documentation for this class was generated from the following file: