Skip to content

Commit

Permalink
Use logging for all logging messages (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdillard authored Dec 21, 2022
1 parent 1ebfcb1 commit ad73077
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Changelog
* Add testing infrastructure
[#41](https://github.com/jdillard/sphinx-sitemap/pull/41)
[#42](https://github.com/jdillard/sphinx-sitemap/pull/42)
* Use logging for all logging messages
[#40](https://github.com/jdillard/sphinx-sitemap/pull/40)

2.2.1
-----
Expand Down
34 changes: 23 additions & 11 deletions sphinx_sitemap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
import os
import xml.etree.ElementTree as ET

from sphinx.util.logging import getLogger

__version__ = "2.3.0"

logger = getLogger(__name__)


def setup(app):
"""Setup connects events to the sitemap builder"""
Expand Down Expand Up @@ -105,17 +109,22 @@ def add_html_link(app, pagename, templatename, context, doctree):
def create_sitemap(app, exception):
"""Generates the sitemap.xml from the collected HTML page links"""
site_url = app.builder.config.site_url or app.builder.config.html_baseurl
site_url = site_url.rstrip("/") + "/"
if not site_url:
print(
"sphinx-sitemap error: neither html_baseurl nor site_url "
"are set in conf.py. Sitemap not built."
if site_url:
site_url.rstrip("/") + "/"
else:
logger.warning(
"sphinx-sitemap: neither html_baseurl nor site_url are set in conf.py."
"Sitemap not built.",
type="sitemap",
subtype="configuration",
)
return

if not app.sitemap_links:
print(
"sphinx-sitemap warning: No pages generated for %s"
% app.config.sitemap_filename
logger.info(
"sphinx-sitemap: No pages generated for %s" % app.config.sitemap_filename,
type="sitemap",
subtype="information",
)
return

Expand Down Expand Up @@ -158,7 +167,10 @@ def create_sitemap(app, exception):
ET.ElementTree(root).write(
filename, xml_declaration=True, encoding="utf-8", method="xml"
)
print(
"%s was generated for URL %s in %s"
% (app.config.sitemap_filename, site_url, filename)

logger.info(
"sphinx-sitemap: %s was generated for URL %s in %s"
% (app.config.sitemap_filename, site_url, filename),
type="sitemap",
subtype="information",
)

0 comments on commit ad73077

Please sign in to comment.