Skip to content

Logging Function Reference

Brad Mostert edited this page Oct 1, 2016 · 1 revision

This page details the code interface to the Logging enhancement. These are all functions that you can call on the BaseCommand class to customize the functionality for one specific command.

Go to the Logging page for general information about this enhancement;

##Table of Contents##

##Functions

getLogger()

Parameters: none
Return Value:
Monolog\Logger
This implements the PSR-3 LoggerInterface and thus using the logger does not necessarily couple your code to the Monolog library.
Description:
This is the primary way to access the Monolog logger that has been configured for your command.
Availability: After initialize

setLogLevel( int $logLevel )

Parameters:

  • logLevel:
    One of the log level constants defined on the Monolog Logger class. For reference these are:

    - Logger::DEBUG  - 100
    - Logger::INFO - 200
    - Logger::NOTICE - 250
    - Logger::WARNING - 300
    - Logger::ERROR - 400
    - Logger::CRITICAL - 500
    - Logger::ALERT - 550
    - Logger::EMERGENCY - 600
    

Return Value:
Afrihost\BaseCommandBundle\Command\BaseCommand
Description:
Allows you to change the logging verbosity for a particular command from the globally defined default. Useful for commands that go through a lifecycle of maturity
Availability:
Anytime. If used after initialize, it will result in a log record that reads LOG LEVEL CHANGED: <level> to help explain why the entire execution did not output with the same verbosity. This is helpful if you only need to increase the verbosity for a subsection of your code.

getLogLevel()

Parameters: none
Return Value:
integer
Corresponds to the log level constants defined on the Monolog Logger class. See the function reference of setLogLevel for more information.
Description:
Get the currently configured logging verbosity.
Availability: Anytime

getLogLevelName()

Parameters: none
Return Value:
string
Corresponds to Monolog's string name for the RFC 5424 log levels. See the description of the --log-level parameter for more information.
Description:
Get the string name of the currently configured logging verbosity.
Availability: Anytime

setLogFilename( string $logFilename )

Parameters:

  • logFilename:
    Name of the file to log to including file extension. This should only contain characters that are valid for your filesystem.

Return Value:
Afrihost\BaseCommandBundle\Command\BaseCommand
Description:
Provide a specific file name to log to rather than using one automatically generated by the name strategy. If the file does not yet exist in the log directory, it will be created otherwise it will be appended. Using this function also overrides the globally configured file_extension so your provided name must include an extension if you want one.
Availability: Before initialize

getLogFilename( bool $fullPath = true )

Parameters:

  • fullPath:
    Whether to return the logfile name with preceded by the directory that it is in or just the name of the file (symmetric to setLogFilename)

Return Value: string
Description:
Returns the name of the file being logged to.
Availability:
If no filename is provided via setLogFilename, the automatic name is only generated during initialize. Behaviour before either of these is undefined

setDefaultLogFileExtension( string $defaultLogFileExtension )

Parameters:

  • defaultLogFileExtension:
    The extension that will be append. This should typically start with a period.

Return Value:
Afrihost\BaseCommandBundle\Command\BaseCommand
Description:
Override the extension of the automatically generated log file name for this command's log file. In most cases you will probably want to set this for all your commands in your config. The file extension is ignored if a specific log file name is provided using the setLogFilename() function.
Availability:
Before initialize

getDefaultLogFileExtension()

Parameters: none
Return Value: string
Description:
Get the file extension added to the automatically generated logfile name.
Availability: Anytime

setLogToConsole( bool $logToConsole )

Parameters:

  • logToConsole:
    TRUE to enable and FALSE to disable

Return Value:
Afrihost\BaseCommandBundle\Command\BaseCommand
Description:
Use this to enable or disable logging to console for this particular command. The default is true (i.e. enabled). This is useful for simplifying the output of commands that also write directly to the OutputInterface object of the command.
Availability:
Before initialize

isLogToConsole()

Parameters: none
Return Value: bool
Description:
Whether or not logging to console is enabled for this command.
Availability: Anytime

setLogToFile( bool $logToFile )

Parameters:

  • logToConsole:
    TRUE to enable and FALSE to disable

Return Value:
Afrihost\BaseCommandBundle\Command\BaseCommand
Description:
Use this to enable or disable logging to file for this particular command. The default is true (i.e. enabled). This is useful for commands that are always run manually and thus do not require a log file.
Availability:
Before initialize

isLogToFile()

Parameters: none
Return Value: bool
Description:
Whether or not logging to file is enabled for this command.
Availability: Anytime

setConsoleLogLineFormat( string $format )

Parameters:

  • format: Any format string with placeholders that is supported by Monolog's LineFormatter. You can also specify null to use the Monolog default which currently is [%datetime%] %channel%.%level_name%: %message% %context% %extra%'.

Return Value:
Afrihost\BaseCommandBundle\Command\BaseCommand
Description:
Provide the string format that the LineFormatter will be configured with when logging to console. A new line character will automatically be appended to the line format provided
Availability:
Before initialize

getConsoleLogLineFormat()

Parameters: none
Return Value: string
Description:
The format configured for the Console Log Handler's LineFormatter
Availability: Anytime

setFileLogLineFormat( string $format )

Parameters:

  • format: Any format string with placeholders that is supported by Monolog's LineFormatter. You can also specify null to use the Monolog default which currently is [%datetime%] %channel%.%level_name%: %message% %context% %extra%'.

Return Value:
Afrihost\BaseCommandBundle\Command\BaseCommand
Description:
Provide the string format that the LineFormatter will be configured with when logging to file. A new line character will automatically be appended to the line format provided
Availability:
Before initialize

getFileLogLineFormat()

Parameters: none
Return Value: string
Description:
The format configured for the File Log Handlers's LineFormatter
Availability: Anytime

setFileLogLineBreaks( bool $fileLogLineBreaks )

Parameters:

  • fileLogLineBreaks: Whether new line characters are outputted to the log entry when logging to file

Return Value:
Afrihost\BaseCommandBundle\Command\BaseCommand
Description:
Override the default setting for striping new line characters from log entries logged to file.
Availability:
Before initialize

getFileLogLineBreaks()

Parameters: none
Return Value: bool
Description:
Whether line breaks are outputted in this command's log file
Availability: Anytime

setConsoleLogLineBreaks( bool $consoleLogLineBreaks )

Parameters:

  • consoleLogLineBreaks: Whether new line characters are outputted to the log entry when logging to console

Return Value:
Afrihost\BaseCommandBundle\Command\BaseCommand
Description:
Override the default setting for striping new line characters from log entries logged to console.
Availability:
Before initialize

getConsoleLogLineBreaks()

Parameters: none
Return Value: bool
Description:
Whether line breaks are outputted in this command's console log
Availability: Anytime

What Next

If you haven't checked out the overview of the Logging Enhancement, now it is a good time