| 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 |
|
| 14 |
import logging |
| 15 |
from logging import NullHandler |
| 16 |
|
| 17 |
from boto3.compat import _warn_deprecated_python |
| 18 |
from boto3.session import Session |
| 19 |
|
| 20 |
__author__ = 'Amazon Web Services' |
| 21 |
__version__ = '1.40.72' |
| 22 |
|
| 23 |
|
| 24 |
# The default Boto3 session; autoloaded when needed. |
| 25 |
DEFAULT_SESSION = None |
| 26 |
|
| 27 |
|
| 28 |
def setup_default_session(**kwargs): |
| 29 |
""" |
| 30 |
Set up a default session, passing through any parameters to the session |
| 31 |
constructor. There is no need to call this unless you wish to pass custom |
| 32 |
parameters, because a default session will be created for you. |
| 33 |
""" |
| 34 |
global DEFAULT_SESSION |
| 35 |
DEFAULT_SESSION = Session(**kwargs) |
| 36 |
|
| 37 |
|
| 38 |
def set_stream_logger(name='boto3', level=logging.DEBUG, format_string=None): |
| 39 |
""" |
| 40 |
Add a stream handler for the given name and level to the logging module. |
| 41 |
By default, this logs all boto3 messages to ``stdout``. |
| 42 |
|
| 43 |
>>> import boto3 |
| 44 |
>>> boto3.set_stream_logger('boto3.resources', logging.INFO) |
| 45 |
|
| 46 |
For debugging purposes a good choice is to set the stream logger to ``''`` |
| 47 |
which is equivalent to saying "log everything". |
| 48 |
|
| 49 |
.. WARNING:: |
| 50 |
Be aware that when logging anything from ``'botocore'`` the full wire |
| 51 |
trace will appear in your logs. If your payloads contain sensitive data |
| 52 |
this should not be used in production. |
| 53 |
|
| 54 |
:type name: string |
| 55 |
:param name: Log name |
| 56 |
:type level: int |
| 57 |
:param level: Logging level, e.g. ``logging.INFO`` |
| 58 |
:type format_string: str |
| 59 |
:param format_string: Log message format |
| 60 |
""" |
| 61 |
if format_string is None: |
| 62 |
format_string = "%(asctime)s %(name)s [%(levelname)s] %(message)s" |
| 63 |
|
| 64 |
logger = logging.getLogger(name) |
| 65 |
logger.setLevel(level) |
| 66 |
handler = logging.StreamHandler() |
| 67 |
handler.setLevel(level) |
| 68 |
formatter = logging.Formatter(format_string) |
| 69 |
handler.setFormatter(formatter) |
| 70 |
logger.addHandler(handler) |
| 71 |
|
| 72 |
|
| 73 |
def _get_default_session(): |
| 74 |
""" |
| 75 |
Get the default session, creating one if needed. |
| 76 |
|
| 77 |
:rtype: :py:class:`~boto3.session.Session` |
| 78 |
:return: The default session |
| 79 |
""" |
| 80 |
if DEFAULT_SESSION is None: |
| 81 |
setup_default_session() |
| 82 |
_warn_deprecated_python() |
| 83 |
|
| 84 |
return DEFAULT_SESSION |
| 85 |
|
| 86 |
|
| 87 |
def client(*args, **kwargs): |
| 88 |
""" |
| 89 |
Create a low-level service client by name using the default session. |
| 90 |
|
| 91 |
See :py:meth:`boto3.session.Session.client`. |
| 92 |
""" |
| 93 |
return _get_default_session().client(*args, **kwargs) |
| 94 |
|
| 95 |
|
| 96 |
def resource(*args, **kwargs): |
| 97 |
""" |
| 98 |
Create a resource service client by name using the default session. |
| 99 |
|
| 100 |
See :py:meth:`boto3.session.Session.resource`. |
| 101 |
""" |
| 102 |
return _get_default_session().resource(*args, **kwargs) |
| 103 |
|
| 104 |
|
| 105 |
# Set up do-nothing logging like a library is supposed to. |
| 106 |
# https://docs.python.org/3.3/howto/logging.html#configuring-logging-for-a-library |
| 107 |
logging.getLogger('boto3').addHandler(NullHandler()) |