Skip to content

Commit

Permalink
slack: fix logger
Browse files Browse the repository at this point in the history
  • Loading branch information
naisanzaa committed Oct 25, 2023
1 parent 1ab0180 commit 221b429
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 35 deletions.
24 changes: 12 additions & 12 deletions automon/integrations/slackWrapper/client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import os
import slack

from automon.log import Logging
from automon.log import logger

from .config import SlackConfig
from .bots import BotInfo
from .error import SlackError

log = logger.logging.getLogger(__name__)
log.setLevel(logger.ERROR)


class SlackClient(SlackConfig):

Expand All @@ -19,8 +22,6 @@ def __init__(self, token: str = None,
"""Slack client
"""

self._log = Logging(SlackClient.__name__, Logging.ERROR)

self.config = config or SlackConfig(token=token, username=username, channel=channel)
self.client = slack.WebClient(token=self.config.token)

Expand All @@ -43,13 +44,12 @@ def _get_bot_info(self):

try:
name = BotInfo(self.client.bots_info()).name
self._log.debug(f'Bot name: {name}')
log.debug(f'Bot name: {name}')
return name
except Exception as e:
error = SlackError(e)
self._log.error(
f'''[{self._get_bot_info.__name__}]\tCouldn't get bot name, missing permission: {error.needed}''',
enable_traceback=False)
log.error(
f'''[{self._get_bot_info.__name__}]\tCouldn't get bot name, missing permission: {error.needed}''')
return ''

return ''
Expand All @@ -63,7 +63,7 @@ def chat_postMessage(self, channel: str, text: str) -> slack.WebClient.chat_post
return SyntaxError

msg = f'{channel} @{self.username}: {text}'
self._log.debug(msg)
log.debug(msg)

try:
response = self.client.chat_postMessage(
Expand All @@ -72,7 +72,7 @@ def chat_postMessage(self, channel: str, text: str) -> slack.WebClient.chat_post
assert response["ok"]
return response
except Exception as e:
self._log.error(e, enable_traceback=False)
log.error(e)

return False

Expand All @@ -96,8 +96,8 @@ def files_upload(self, file, filename=None):

# check if file exists
if not os.path.isfile(file):
self._log.error(f'File not found: {file}')
self._log.error(f'Working dir: {os.getcwd()}')
log.error(f'File not found: {file}')
log.error(f'Working dir: {os.getcwd()}')
return False

# get filename
Expand All @@ -116,6 +116,6 @@ def files_upload(self, file, filename=None):
file=file, filename=filename, title=title, username=self.username, channels=self.channel)

assert response["ok"]
self._log.debug(f'File uploaded: {file} ({file_size}B) ({self.username}')
log.debug(f'File uploaded: {file} ({file_size}B) ({self.username}')

return response
31 changes: 15 additions & 16 deletions automon/integrations/slackWrapper/clientAsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import random
import asyncio

from automon.log import Logging
from automon.log import logger
from automon.helpers.nest_asyncioWrapper import AsyncStarter

from .config import ConfigSlack
from .bots import BotInfo
from .error import SlackError

log = logger.logging.getLogger(__name__)
log.setLevel(logger.ERROR)


class SlackAsyncClient(ConfigSlack):

Expand All @@ -18,8 +21,6 @@ def __init__(self, token: str = ConfigSlack.slack_token, username: str = '',
"""Slack Async client
"""

self._log = Logging(SlackAsyncClient.__name__, Logging.ERROR)

self.token = ConfigSlack.slack_token or token
self.client = slack.WebClient(token=token)

Expand Down Expand Up @@ -49,13 +50,12 @@ def _get_bot_info(self):

try:
name = BotInfo(self.client.bots_info()).name
self._log.debug(f'Bot name: {name}')
log.debug(f'Bot name: {name}')
return name
except Exception as e:
error = SlackError(e)
self._log.error(
f'''[{self._get_bot_info.__name__}]\tCouldn't get bot name, missing permission: {error.needed}''',
enable_traceback=False)
log.error(
f'''[{self._get_bot_info.__name__}]\tCouldn't get bot name, missing permission: {error.needed}''')
return ''

return ''
Expand Down Expand Up @@ -98,8 +98,8 @@ def files_upload(self, file, filename=None):

# check if file exists
if not os.path.isfile(file):
self._log.error(f'File not found: {file}')
self._log.error(f'Working dir: {os.getcwd()}')
log.error(f'File not found: {file}')
log.error(f'Working dir: {os.getcwd()}')
return False

# get filename
Expand All @@ -119,7 +119,7 @@ def files_upload(self, file, filename=None):

assert response["ok"]

self._log.debug(f'File uploaded: {file} ({file_size}B) ({self.username}')
log.debug(f'File uploaded: {file} ({file_size}B) ({self.username}')

return response

Expand All @@ -138,7 +138,7 @@ async def _consumer(self):

while self.connected:
try:
self._log.debug(msg)
log.debug(msg)
response = self.client.chat_postMessage(
text=text, channel=channel, username=self.username,
icon_emoji=self.icon_emoji, icon_url=self.icon_url)
Expand All @@ -149,10 +149,10 @@ async def _consumer(self):
await asyncio.sleep(random.choice(range(2)))
else:
sleep = random.choice(range(4))
self._log.debug(f'sleeping {sleep}, queue size is {self.queue.qsize()}')
log.debug(f'sleeping {sleep}, queue size is {self.queue.qsize()}')
await asyncio.sleep(sleep)

self._log.debug(f'Burst: {burst}, Retry: {retry}, Queue {self.queue.qsize()}')
log.debug(f'Burst: {burst}, Retry: {retry}, Queue {self.queue.qsize()}')

burst += 1
retry = 0
Expand All @@ -165,7 +165,6 @@ async def _consumer(self):
retry += 1
burst_max = burst
error = SlackError(e)
self._log.error(
f'{self._consumer.__name__}\t{error.error}\t{msg}\tRetry: {retry}, Burst max: {burst_max}',
enable_traceback=False)
log.error(
f'{self._consumer.__name__}\t{error.error}\t{msg}\tRetry: {retry}, Burst max: {burst_max}')
burst = 0
5 changes: 3 additions & 2 deletions automon/integrations/slackWrapper/config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os

from automon.log import Logging
from automon.log import logger

log = Logging(name=__name__, level=Logging.ERROR)
log = logger.logging.getLogger(__name__)
log.setLevel(logger.ERROR)


class SlackConfig(object):
Expand Down
12 changes: 7 additions & 5 deletions automon/integrations/slackWrapper/error.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from automon.log import Logging
from automon.log import logger

log = logger.logging.getLogger(__name__)
log.setLevel(logger.ERROR)


class SlackError:
Expand All @@ -13,7 +16,6 @@ def __init__(self, error: Exception):
}
"""

self._log = Logging(name=SlackError.__name__, level=Logging.ERROR)

self._error = error
self._reason = getattr(self._error, 'reason', '')
Expand Down Expand Up @@ -48,7 +50,7 @@ def error(self):
if self._reason:
return self.strerror

self._log.warn(f'{NotImplemented}')
log.warning(f'{NotImplemented}')
return f'{self._error}'

def needed(self):
Expand All @@ -58,7 +60,7 @@ def needed(self):
if self._reason:
return self.strerror

self._log.warn(f'{NotImplemented}')
log.warning(f'{NotImplemented}')
return f'{self._error}'

def __repr__(self):
Expand All @@ -68,7 +70,7 @@ def __repr__(self):
if self._reason:
return f'{self.strerror}'

self._log.warn(f'{NotImplemented}')
log.warning(f'{NotImplemented}')
return f'{self._error}'

def __str__(self):
Expand Down

0 comments on commit 221b429

Please sign in to comment.