<p>You could write a wrapper log method that's abstracts the check away so you only have to do it once.  The update your code to use the wrapper.</p>

<p>That method could just use <code>craft()-&gt;plugins-&gt;getPlugin('pluginHandle')</code> to see if the target plugin is installed and enabled.  Craft will auto-load the plugin's services, so you don't need to check specifically for them.</p>

CC BY-SA 3.0
2016-05-17T23:38:11.947Z
That's a good idea thanks Brad. I could override BasePlugin::log() in these other plugins and have it use WatchTower or gracefully fall back to use BasePlugin::log() if WatchTower isn't installed. That's fine for my use but not ideal if another developer was using it though. I'll think on it some more. Thanks for your time!
JamesNZ
CC BY-SA 3.0
2016-05-17T22:35:44.693Z
2016-05-17T22:35:44.693Z
Brad Bell
<p>I have a Craft Plugin that is a <a href="https://github.com/JamesMcFall/WatchTower-CraftCMS" rel="nofollow">logging library</a> I want to use in other plugins I develop, but I don't want it to be a dependancy in these other plugins. Is there a nice, space efficient way in Craft to do something like below? I don't want to have to wrap every call in an if statement:</p>

<pre><code># If WatchTower isn't installed... don't explode please!!!
@craft()-&gt;watchTowerDB-&gt;log($params)
</code></pre>

<p>My plugin seems to stop processing and redirect to the current page if it isn't installed. Thanks!</p>

<plugin-development><plugins>
Safely calling other plugin code from a different plugin
# Safely calling other plugin code from a different plugin

(Asked by: username_0 on 2016-05-17)

I have a Craft Plugin that is a logging library I want to use in other plugins I develop, but I don't want it to be a dependancy in these other plugins. Is there a nice, space efficient way in Craft to do something like below? I don't want to have to wrap every call in an if statement:
# If WatchTower isn't installed... don't explode please!!!
@craft()->watchTowerDB->log($params)

My plugin seems to stop processing and redirect to the current page if it isn't installed. Thanks!

**Tags:** plugin-development, plugins

---

## 1 answers

### Answer by username_1 on 2016-05-17. Score: 3 | Accepted answer

You could write a wrapper log method that's abstracts the check away so you only have to do it once. The update your code to use the wrapper.

That method could just use `craft()->plugins->getPlugin('pluginHandle')` to see if the target plugin is installed and enabled. Craft will auto-load the plugin's services, so you don't need to check specifically for them.

> **Comments:**
> **username_0**: That's a good idea thanks Brad. I could override BasePlugin::log() in these other plugins and have it use WatchTower or gracefully fall back to use BasePlugin::log() if WatchTower isn't installed. That's fine for my use but not ideal if another developer was using it though. I'll think on it some more. Thanks for your time!  

---

