Repository: pydicom/pydicom

Issue:
Cannot control/except processing tag error in latest version
**Describe the bug**
When I was working with a large number of dcm files, some of the elements in the dcm didn't conform to the specification (which was out of my control), and then when my script ran, it would generate a lot of error logs.
![image](https://github.com/pydicom/pydicom/assets/34393671/618315f3-abc3-4bc7-9f76-21e502045209)

**Expected behavior**
I feel like I should be able to catch exceptions or control logs.

**Steps To Reproduce**
```python
    metadata_dict = {}
    try:
        metadata_dict.update(dcm.file_meta.to_json_dict(suppress_invalid_tags=True))
        metadata_dict.update(dcm.to_json_dict(suppress_invalid_tags=True))
    except ValueError as e:
        logging.error(f"Error occurred at tag: {e.args[0]} file: {metadata_file}")
    except Exception as e:
        logging.error(f"{e}: {metadata_file}")
```

**Your environment**
```bash
$  python3.9 -m pydicom.env_info
module       | version
------       | -------
platform     | Linux-5.15.0-1049-azure-x86_64-with-glibc2.31
Python       | 3.9.18 (main, Aug 25 2023, 13:20:04)  [GCC 9.4.0]
pydicom      | 2.4.2
gdcm         | _module not found_
jpeg_ls      | _module not found_
numpy        | 1.26.0
PIL          | 10.1.0
pylibjpeg    | 1.4.0
openjpeg     | _module not found_
libjpeg      | 1.3.4
```


Discussion:
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/pydicom/pydicom/pull/2089?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydicom) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 98.32%. Comparing base [(`90a6376`)](https://app.codecov.io/gh/pydicom/pydicom/commit/90a6376a284c397c58790d35366063827711e685?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydicom) to head [(`c38d55f`)](https://app.codecov.io/gh/pydicom/pydicom/commit/c38d55f0fbe82d87717c35113e4753cf114f0151?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydicom).

> :exclamation: **Current head c38d55f differs from pull request most recent head 7b7c29c**
> 
> Please [upload](https://docs.codecov.com/docs/codecov-uploader) reports for the commit 7b7c29c to get more accurate results.

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main    #2089   +/-   ##
=======================================
  Coverage   98.32%   98.32%           
=======================================
  Files          76       76           
  Lines       13045    13046    +1     
=======================================
+ Hits        12827    12828    +1     
  Misses        218      218           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/pydicom/pydicom/pull/2089?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydicom).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydicom).


Patch:
diff --git a/doc/release_notes/v3.0.0.rst b/doc/release_notes/v3.0.0.rst
index c9d67b02e..179604818 100644
--- a/doc/release_notes/v3.0.0.rst
+++ b/doc/release_notes/v3.0.0.rst
@@ -64,6 +64,9 @@ Changes
   exception as Pillow cannot decode such data correctly (:issue:`2006`)
 * An exception will now be raised if an :class:`~numpy.ndarray` is used to set
   *Pixel Data* (:issue:`50`)
+* Logging of errors when converting elements using :meth:`Dataset.to_json_dict()
+  <pydicom.dataset.Dataset.to_json_dict>` have been made more verbose and now use
+  ``logging.WARNING`` (:issue:`1909`).
 
 
 Removals
diff --git a/src/pydicom/dataset.py b/src/pydicom/dataset.py
index 55900a1e5..fb9cbf261 100644
--- a/src/pydicom/dataset.py
+++ b/src/pydicom/dataset.py
@@ -3145,10 +3145,12 @@ class Dataset:
                         bulk_data_threshold=bulk_data_threshold,
                     )
                 except Exception as exc:
-                    logger.error(f"Error while processing tag {json_key}")
                     if not suppress_invalid_tags:
+                        logger.error(f"Error while processing tag {json_key}")
                         raise exc
 
+                    logger.warning(f"Error while processing tag {json_key}: {exc}")
+
         return json_dataset
 
     def to_json(


Test patch:
diff --git a/tests/test_json.py b/tests/test_json.py
index e7a885533..1b33011cb 100644
--- a/tests/test_json.py
+++ b/tests/test_json.py
@@ -1,5 +1,6 @@
 # Copyright 2008-2019 pydicom authors. See LICENSE file for details.
 import json
+import logging
 from unittest import mock
 
 import pytest
@@ -300,7 +301,7 @@ class TestDataSetToJson:
 
         assert "00100010" not in ds_json
 
-    def test_suppress_invalid_tags_with_failed_dataelement(self):
+    def test_suppress_invalid_tags_with_failed_dataelement(self, caplog):
         """Test tags that raise exceptions don't if suppress_invalid_tags True."""
         ds = Dataset()
         # we have to add a RawDataElement as creating a DataElement would
@@ -309,8 +310,13 @@ class TestDataSetToJson:
             Tag(0x00082128), "IS", 4, b"5.25", 0, True, True
         )
 
-        ds_json = ds.to_json_dict(suppress_invalid_tags=True)
-        assert "00082128" not in ds_json
+        with caplog.at_level(logging.WARNING, logger="pydicom"):
+            ds_json = ds.to_json_dict(suppress_invalid_tags=True)
+            assert "00082128" not in ds_json
+
+        assert (
+            "Error while processing tag 00082128: Invalid value for VR IS: '5.25'"
+        ) in caplog.text
 
 
 class TestSequence: