__init__.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"). You
  4. # may not use this file except in compliance with the License. A copy of
  5. # the License is located at
  6. #
  7. # https://aws.amazon.com/apache2.0/
  8. #
  9. # or in the "license" file accompanying this file. This file is
  10. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  11. # ANY KIND, either express or implied. See the License for the specific
  12. # language governing permissions and limitations under the License.
  13. import logging
  14. from boto3.compat import _warn_deprecated_python
  15. from boto3.session import Session
  16. __author__ = 'Amazon Web Services'
  17. __version__ = '1.28.55'
  18. # The default Boto3 session; autoloaded when needed.
  19. DEFAULT_SESSION = None
  20. def setup_default_session(**kwargs):
  21. """
  22. Set up a default session, passing through any parameters to the session
  23. constructor. There is no need to call this unless you wish to pass custom
  24. parameters, because a default session will be created for you.
  25. """
  26. global DEFAULT_SESSION
  27. DEFAULT_SESSION = Session(**kwargs)
  28. def set_stream_logger(name='boto3', level=logging.DEBUG, format_string=None):
  29. """
  30. Add a stream handler for the given name and level to the logging module.
  31. By default, this logs all boto3 messages to ``stdout``.
  32. >>> import boto3
  33. >>> boto3.set_stream_logger('boto3.resources', logging.INFO)
  34. For debugging purposes a good choice is to set the stream logger to ``''``
  35. which is equivalent to saying "log everything".
  36. .. WARNING::
  37. Be aware that when logging anything from ``'botocore'`` the full wire
  38. trace will appear in your logs. If your payloads contain sensitive data
  39. this should not be used in production.
  40. :type name: string
  41. :param name: Log name
  42. :type level: int
  43. :param level: Logging level, e.g. ``logging.INFO``
  44. :type format_string: str
  45. :param format_string: Log message format
  46. """
  47. if format_string is None:
  48. format_string = "%(asctime)s %(name)s [%(levelname)s] %(message)s"
  49. logger = logging.getLogger(name)
  50. logger.setLevel(level)
  51. handler = logging.StreamHandler()
  52. handler.setLevel(level)
  53. formatter = logging.Formatter(format_string)
  54. handler.setFormatter(formatter)
  55. logger.addHandler(handler)
  56. def _get_default_session():
  57. """
  58. Get the default session, creating one if needed.
  59. :rtype: :py:class:`~boto3.session.Session`
  60. :return: The default session
  61. """
  62. if DEFAULT_SESSION is None:
  63. setup_default_session()
  64. _warn_deprecated_python()
  65. return DEFAULT_SESSION
  66. def client(*args, **kwargs):
  67. """
  68. Create a low-level service client by name using the default session.
  69. See :py:meth:`boto3.session.Session.client`.
  70. """
  71. return _get_default_session().client(*args, **kwargs)
  72. def resource(*args, **kwargs):
  73. """
  74. Create a resource service client by name using the default session.
  75. See :py:meth:`boto3.session.Session.resource`.
  76. """
  77. return _get_default_session().resource(*args, **kwargs)
  78. # Set up logging to ``/dev/null`` like a library is supposed to.
  79. # https://docs.python.org/3.3/howto/logging.html#configuring-logging-for-a-library
  80. class NullHandler(logging.Handler):
  81. def emit(self, record):
  82. pass
  83. logging.getLogger('boto3').addHandler(NullHandler())