Skip to content

Commit

Permalink
[#13438] - Added "excludeAdapters" functionality for the logger
Browse files Browse the repository at this point in the history
  • Loading branch information
niden committed Dec 7, 2018
1 parent feb6dd9 commit 30643d1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
41 changes: 26 additions & 15 deletions phalcon/logger.zep
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class Logger implements LoggerInterface
*/
public function excludeAdapters(array adapters = []) -> <Logger>
{
var adapter, registered, excluded;
var name, registered;

let registered = this->getAdapters();

Expand All @@ -164,12 +164,10 @@ class Logger implements LoggerInterface
* the registered adapters. If they match, add them to the
* this->excluded array
*/
for adapter in adapters {
if typeof adapter === "string" {
if fetch excluded, registered[adapter] {
let this->excluded[] = excluded;
}
}
for name, _ in adapters {
if isset(registered[name]) {
let this->excluded[] = name;
}
}

return this;
Expand Down Expand Up @@ -272,15 +270,19 @@ class Logger implements LoggerInterface
*/
public function removeAdapter(string name) -> <Logger>
{
var adapter;
var adapters;

let adapters = this->adapters;

if !fetch adapter, this->adapters[name] {
if true !== isset(adapters[name]) {
throw new Exception("Adapter does not exist for this logger");
}

unset(this->adapters[name]);
unset adapters[name];

return adapter;
let this->adapters = adapters;

return this;
}

/**
Expand Down Expand Up @@ -325,9 +327,12 @@ class Logger implements LoggerInterface
*/
protected function addMessage(int level, string message, array context = []) -> bool
{
var adapter, item, levelName, levels;
var adapter, key, keys, excluded, levelName, levels, item, registered;

let registered = this->adapters,
excluded = this->excluded;

if count(this->adapters) === 0 {
if count(registered) === 0 {
throw new Exception("No adapters specified");
}

Expand All @@ -339,9 +344,15 @@ class Logger implements LoggerInterface
let item = new Item(message, levelName, level, time(), context);

/**
* @todo Check if any adapters were excluded and log only to them
* Compare the actual adapters array with the excluded one. Whatever
* the difference is, that is the array of adapters that we will log
* this message to. By default `excluded` is empty so the message will
* be loggged to all registered adapters
*/
for adapter in this->adapters {
let keys = array_diff(array_keys(registered), excluded);

for key in keys {
let adapter = registered[key];
adapter->process(item);
}

Expand Down
5 changes: 2 additions & 3 deletions phalcon/logger/adapter/file.zep
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,12 @@ class File extends AbstractAdapter
*/
public function __construct(string! name, array options = [])
{
string mode;
var mode;

/**
* Mode
*/
if isset(options["mode"]) {
let mode = (string) options["mode"];
if fetch mode, options["mode"] {
if memstr(mode, "r") {
throw new Exception("Adapter cannot be opened in read mode");
}
Expand Down

0 comments on commit 30643d1

Please sign in to comment.