mod_servlet
C++Servlets
|
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< logger > | log (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... | |
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: console
- redirects logging output to std::cout
file
- redirects logging output to file. Uses the following configuration parameters: 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"
"%y"
- resolves to last two digits of a current year; "%Y"
- resolves to four digits of a current year; "%m"
- resolves to two digits of a current month; "%d"
- resolves to two digits of a current day; "%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
; 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: 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. 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. date-size-file
- redirects logging output to file and uses rotation by both date and size. output.handler
is console
prefix.printer
- specifies prefix printer to be used. Currently the following printer types are supported: simple
- simple prefix printer which prints the line prefix accordingly to the following parameters: 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: %TIME%
- current timestamp (as prescribed by "timestamp.format"
parameter); %THREAD%
- current thread id; %LEVEL%
- current logging level; %NAME%
- logger name; "%TIME% | %THREAD% | %LEVEL% | %NAME% | "
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.
|
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:
And after this it can read configuration with prefix "sys."
name | New log_output_factory name. |
fac | log_output factory. |
|
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:
And after this it can read configuration with prefix "color."
name | New prefix_printer_factory name. |
fac | prefix_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.
name | logger name |
|
inline |
Returns current synchronization policy used by this registry.
|
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.
loc | New 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.
name | Logger 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.
config_file_name | Name of configuration file. |
base_dir | Base directory to use to resolve relative paths. |
update_loggers | If 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.
props | Properties map. |
base_dir | Base directory to use to resolve relative paths. |
update_loggers | If 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.
update_prefix_printer | if true updates prefix_printer for loggers. |
update_log_output | if true updates log_output for loggers. |
update_locale | if 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.
base_dir | New 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.
base_dir | New base directory. |
|
inline |
Sets new global logging level.
level | Level to set. |
void servlet::logging::log_registry::set_log_output | ( | log_output * | log_out | ) |
Sets new log_output
for this registry.
log_out | New log_output to be used with this registry. |
|
inline |
Sets new prefix_printer
for this registry.
pp | new 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.
sync_policy | new synchronization polisy to set. |
configu_exception | is thrown if loggers has already been created. |