Skip to content

Commit

Permalink
Do not use deprecated utcnow in ipython log line: (#895)
Browse files Browse the repository at this point in the history
a = datetime.datetime.now(datetime.UTC).replace(tzinfo=None).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
b = datetime.datetime.now(datetime.UTC).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
c = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ")

print("\n".join((a,b,c)))

\# 2024-04-29T19:06:01.275418Z
\# 2024-04-29T19:06:01.275460Z
\# 2024-04-29T19:06:01.275501Z
  • Loading branch information
salomon-smekecohen authored Apr 29, 2024
1 parent 9981b9e commit 08af744
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion baseplate/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,11 @@ def load_and_run_shell() -> None:
# IPython 4 and below
from IPython import Config

# test for UTC availability from the datetime package
import datetime as dt

datetime_exec = "now(datetime.UTC)" if getattr(dt, "UTC", False) else "utcnow()"

ipython_config = Config()
ipython_config.InteractiveShellApp.exec_lines = [
# monkeypatch IPython's log-write() to enable formatted input logging, copying original code:
Expand All @@ -451,7 +456,7 @@ def log_write(self, data, kind="input", message_id="IEXC"):
write = self.logfile.write
if kind=='input':
# Generate an RFC 5424 compliant syslog format
write(f'<13>1 {{datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ")}} {{os.uname().nodename}} baseplate-shell {{os.getpid()}} {{message_id}} - {{data}}')
write(f'<13>1 {{datetime.datetime.{datetime_exec}.strftime("%Y-%m-%dT%H:%M:%S.%fZ")}} {{os.uname().nodename}} baseplate-shell {{os.getpid()}} {{message_id}} - {{data}}')
elif kind=='output' and self.log_output:
odata = u'\\n'.join([u'#[Out]# %s' % s
for s in data.splitlines()])
Expand Down

0 comments on commit 08af744

Please sign in to comment.