Logger

Coaster can help your application log errors at run-time. Initialize with coaster.logger.init_app(). If you use coaster.app.init_app(), this is done automatically for you.

class coaster.logger.LocalVarFormatter(fmt=None, datefmt=None)[source]

Custom log formatter that logs the contents of local variables in the stack frame.

format(record)[source]

Format the specified record as text. Overrides logging.Formatter.format() to remove cache of record.exc_text unless it was produced by this formatter.

formatException(ei)[source]

Format and return the specified exception information as a string.

This default implementation just uses traceback.print_exception()

class coaster.logger.SMSHandler(app_name, exotel_sid, exotel_token, exotel_from, twilio_sid, twilio_token, twilio_from, phonenumbers)[source]

Custom logging handler to send SMSes to admins

emit(record)[source]

Do whatever it takes to actually log the specified logging record.

This version is intended to be implemented by subclasses and so raises a NotImplementedError.

class coaster.logger.SlackHandler(app_name, webhooks)[source]

Custom logging handler to post error reports to Slack.

emit(record)[source]

Do whatever it takes to actually log the specified logging record.

This version is intended to be implemented by subclasses and so raises a NotImplementedError.

coaster.logger.configure(app)

Enables logging for an app using LocalVarFormatter. Requires the app to be configured and checks for the following configuration parameters. All are optional:

  • LOGFILE: Name of the file to log to (default error.log)
  • ADMINS: List of email addresses of admins who will be mailed error reports
  • MAIL_DEFAULT_SENDER: From address of email. Can be an address or a tuple with name and address
  • MAIL_SERVER: SMTP server to send with (default localhost)
  • MAIL_USERNAME and MAIL_PASSWORD: SMTP credentials, if required
  • SLACK_LOGGING_WEBHOOKS: If present, will send error logs to all specified Slack webhooks
  • ADMIN_NUMBERS: List of mobile numbers of admin to send SMS alerts. Requires the following values too
  • SMS_EXOTEL_SID: Exotel SID for Indian numbers (+91 prefix)
  • SMS_EXOTEL_TOKEN: Exotel token
  • SMS_EXOTEL_FROM: Exotel sender’s number
  • SMS_TWILIO_SID: Twilio SID for non-Indian numbers
  • SMS_TWILIO_TOKEN: Twilio token
  • SMS_TWILIO_FROM: Twilio sender’s number

Format for SLACK_LOGGING_WEBHOOKS:

SLACK_LOGGING_WEBHOOKS = [{
    'levelnames': ['WARNING', 'ERROR', 'CRITICAL'],
    'url': 'https://hooks.slack.com/...'
    }]
coaster.logger.init_app(app)[source]

Enables logging for an app using LocalVarFormatter. Requires the app to be configured and checks for the following configuration parameters. All are optional:

  • LOGFILE: Name of the file to log to (default error.log)
  • ADMINS: List of email addresses of admins who will be mailed error reports
  • MAIL_DEFAULT_SENDER: From address of email. Can be an address or a tuple with name and address
  • MAIL_SERVER: SMTP server to send with (default localhost)
  • MAIL_USERNAME and MAIL_PASSWORD: SMTP credentials, if required
  • SLACK_LOGGING_WEBHOOKS: If present, will send error logs to all specified Slack webhooks
  • ADMIN_NUMBERS: List of mobile numbers of admin to send SMS alerts. Requires the following values too
  • SMS_EXOTEL_SID: Exotel SID for Indian numbers (+91 prefix)
  • SMS_EXOTEL_TOKEN: Exotel token
  • SMS_EXOTEL_FROM: Exotel sender’s number
  • SMS_TWILIO_SID: Twilio SID for non-Indian numbers
  • SMS_TWILIO_TOKEN: Twilio token
  • SMS_TWILIO_FROM: Twilio sender’s number

Format for SLACK_LOGGING_WEBHOOKS:

SLACK_LOGGING_WEBHOOKS = [{
    'levelnames': ['WARNING', 'ERROR', 'CRITICAL'],
    'url': 'https://hooks.slack.com/...'
    }]