Logging#
This example showcases the logging capabilities of PyWorkbench.
[1]:
import pathlib
import os
from ansys.workbench.core import launch_workbench
First, import the necessary modules. We import pathlib
for handling filesystem paths and os
for interacting with the operating system. The launch_workbench
function from ansys.workbench.core
is imported to start a Workbench session.
Next, launch a Workbench session using PyWorkbench. Different directories are declared, including the client and server working directories, which should NOT be the same.
[2]:
workdir = pathlib.Path("__file__").parent
server_workdir = workdir / "server_workdir"
client_workdir = workdir / "client_workdir"
alternative_target_dir = workdir / "alternative_target_dir"
Here, we define several directories that will be used during the session. workdir
is set to the directory containing the current file. server_workdir
, client_workdir
, and alternative_target_dir
are subdirectories within the working directory.
Launch Workbench using previous directories:
[3]:
wb = launch_workbench(server_workdir=str(server_workdir.absolute()), client_workdir=str(client_workdir.absolute()))
The launch_workbench
function is called to start a Workbench session. server_workdir
and client_workdir
are set to their absolute paths to avoid any ambiguity in directory locations.
[4]:
downloaded1 = wb.download_file('server1.*')
Downloading server1._.zip: 100%|██████████| 337/337 [00:01<00:00, 331B/s]
This command demonstrates how to download files from the server using a wildcard. The download_file
method is used to fetch all files matching the pattern server1.*
from the server to the client.
[5]:
downloaded2 = wb.download_file('*', target_dir=alternative_target_dir)
Downloading _.zip: 100%|██████████| 652/652 [00:01<00:00, 627B/s]
This command downloads the entire contents of the server directory to an alternative local directory specified by alternative_target_dir
.
[6]:
wb.upload_file('*.txt', 'model?.prt')
Uploading cylinder.txt: 100%|██████████| 10.0/10.0 [00:00<00:00, 640B/s]
Uploading longbar.txt: 100%|██████████| 9.00/9.00 [00:00<?, ?B/s]
Uploading model3.prt: 100%|██████████| 8.00/8.00 [00:00<?, ?B/s]
Uploading model7.prt: 100%|██████████| 8.00/8.00 [00:00<00:00, 512B/s]
This command shows how to upload files to the server using wildcards. All .txt
files and files matching the pattern model?.prt
in the client directory are uploaded to the server.
[7]:
wb.upload_file(os.path.join(alternative_target_dir, 'app.py'), 'non_existing_file1', 'non_existing_file2', show_progress=False)
WARNING:root:The following files do not exist and are skipped: C:\Users\ansys\actions-runner\_work\pyworkbench-examples\pyworkbench-examples\pyworkbench-examples\doc\source\examples\logging\client_workdir\alternative_target_dir\app.py
C:\Users\ansys\actions-runner\_work\pyworkbench-examples\pyworkbench-examples\pyworkbench-examples\doc\source\examples\logging\client_workdir\non_existing_file1
C:\Users\ansys\actions-runner\_work\pyworkbench-examples\pyworkbench-examples\pyworkbench-examples\doc\source\examples\logging\client_workdir\non_existing_file2
Here, files are uploaded from an alternative directory, and non-existing files are specified. The show_progress
parameter is set to False
to disable the progress bar during the upload.
[8]:
export_path = 'wb_log_file.log'
wb.set_log_file(export_path)
print(wb.run_script_file('wb.wbjn', log_level='info'))
ERROR:root:Error when running the script: CommandFailedException: Template Thermal not found in Project.
None
This segment sets up a log file for the script execution. The set_log_file
method directs the logs to wb_log_file.log
, and run_script_file
executes a script with info
log level. The output of the script is printed to the console.
[9]:
wb.reset_log_file()
wb.set_console_log_level('info')
print(wb.run_script_file('wb.wbjn', log_level='info'))
ERROR:root:Error when running the script: CommandFailedException: Template Thermal not found in Project.
None
To change the logging configuration, we first disable the log file using reset_log_file
. The console log level is then set to info
using set_console_log_level
. The script is run again with the same log level, and the output is printed.
[10]:
wb.set_console_log_level('error')
print(wb.run_script_file('wb.wbjn', log_level='info'))
ERROR:root:Error when running the script: CommandFailedException: Template Thermal not found in Project.
None
In this step, the console log level is set to error
, making the logging more restrictive. The script is executed again, and only error-level logs are shown.
[11]:
wb.exit()