protected static function getInstance() { if (!static::$log) { static::$log = new Log(); } return static::$log; }
public static function init() { if (static::$log === null) { static::$log = new Logger('simple'); static::$log->pushHandler(new StreamHandler(BASE_PATH . '/logs/simple.log', Logger::DEBUG)); } }
/** * Get the logger instance. * * @return \Monolog\Logger */ public static function getLoggerInstance() { if (static::$log === null) { static::$log = new Logger('elasticsearch-indexer'); static::$log->pushHandler(new StreamHandler(static::getFilePath(), Logger::ERROR)); } return static::$log; }
public function __construct() { $config = static::$config->log['slack']; $file_path = RESERVAT_DIR . '/../log'; static::$log = new Logger('bookings'); if (static::$config->LOG_LEVEL < Logger::ERROR) { static::$log->pushHandler(new SlackHandler(static::$config->LOG_SLACK_TOKEN, static::$config->LOG_SLACK_CHANNEL, static::$config->LOG_SLACK_USERNAME, true, static::$config->LOG_SLACK_EMOTE, Logger::ERROR)); } if (static::$config->LOG_LEVEL < Logger::DEBUG) { if ($file_path) { if (!is_dir($file_path)) { mkdir($file_path); } static::$log->pushHandler(new StreamHandler($file_path . '/bookings.log', Logger::DEBUG)); } } static::$instance = $this; }
/** * Create a new connection * * @param array $cfg config */ public static function connect(array $cfg) { self::$config = $cfg; self::checkConfig($cfg); try { self::$con = new PDO($cfg['driver'] . ":host=" . $cfg['hostname'] . ";charset=" . $cfg['charset'], $cfg['username'], $cfg['password'], array(PDO::MYSQL_ATTR_LOCAL_INFILE => true, PDO::ATTR_TIMEOUT => 60, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); } catch (PDOException $e) { throw new Exceptions\DataBaseException("You can't connect to the host: " . $e->getMessage()); } self::select($cfg['database']); switch ($cfg['driver']) { case 'mysql': self::$sql = 'MYSQL'; break; } Schema::ini(); if ($cfg['restore'] > 0) { self::iniRestore(); } self::startLog(); static::$log = []; }
public static function set_logger($log) { static::$log = $log; }
/** * Log a message with severity DEBUG * * @param mixed $arg,... A string, exception or format-string + substitutions */ public static function debug() { if (static::$instance !== null && func_num_args() > 0) { static::$instance->log(static::DEBUG, static::formatMessage(func_get_args())); } }
/** * Set log defaults if not yet present * * @action init */ public function maybe_set_defaults() { $defaults = ['datetime', 'fqdn', 'site_url', 'account_id', 'user_email', 'locale', 'wp_version']; if (!array_diff_key($defaults, static::$log)) { return; } $defaults = ['datetime' => gmdate('c'), 'fqdn' => gethostname(), 'site_url' => get_option('siteurl'), 'account_id' => exec('whoami'), 'user_email' => get_userdata(1)->user_email, 'locale' => ($locale = get_option('WPLANG')) ? $locale : 'en_US', 'wp_version' => get_bloginfo('version'), 'wpem_version' => wpem()->version]; static::$log = $defaults; $this->save(); new Geodata($this); // Saves to log }
/** * Receives an instance of Net_SSH2 that is already logged in * to a router manager, and executes commands in the specified * node. * * @param Net_SSH2 &$ssh * Instance returned by CiscoSSH::getSSH function. * @param object $node * Network Element to log in from main SSH device. * e.g. * (object)[ * 'line' => 'ssh', // or 'telnet' * 'authentication'=> 'Password', // or 'Username:Passsword' * 'username' => 'user', * 'password' => 'pass', * 'ip_address' => '172.16.0.1', * ] * @param string|array $commands * String or array of strings to be executed. * @param string? &$hostname * Will contain the configured hostname of the specified node upon returning. * * @return string $output Command(s)'s output from device, slightly tampered with. * @author Ronald Rey **/ public static final function exec(&$ssh, $node, $commands, &$hostname = null) { static::$log = ''; // Capturing the hostname with a Regex pattern matching // any string up to a prompt character, in this case either # or >. $entry_string = $ssh->read("/(#|>)/", NET_SSH2_READ_REGEX); preg_match('/(.*)(#|>)/', $entry_string, $manager_hostname_matches); $manager_hostname = isset($manager_hostname_matches[1]) ? $manager_hostname_matches[1] : null; // Connecting to NE by typing "telnet|ssh IP_ADDRESS" $ssh->write("{$node->line} {$node->ip_address}\n"); // Typing the Username and Password (telnet) or just Password (ssh) if ($node->authentication == "Username:Password") { $ssh->write("{$node->username}\n{$node->password}\n"); } elseif ($node->authentication == "Password") { $ssh->write("{$node->password}\n"); } else { throw new Exception('Tipo de autenticación desconodia.'); } // Reading and storing the welcome string up // to the prompt or the Authentication failed message $welcome_string = $ssh->read("/(#|>)|(Authentication failed.)/", NET_SSH2_READ_REGEX); // Capturing possible responses of the welcome string after our login attempt // and acting accordingly. All of the following are unwanted responses. if (strpos($welcome_string, "% Connection timed out; remote host not responding") !== false || strpos($welcome_string, "% Connection refused by remote host") !== false) { static::log("{$node->ip_address} host down" . PHP_EOL); } elseif (strpos($welcome_string, "Authentication failed") !== false) { static::log("{$node->ip_address} Authentication failed." . PHP_EOL); $ssh->write(""); $ssh->read("/(#|>)/", NET_SSH2_READ_REGEX); continue; } // Capturing the hostname with a Regex pattern matching // any string up to a prompt character, in this case either # or >. preg_match('/(.*)(#|>)/', $welcome_string, $hostname_matches); $hostname = isset($hostname_matches[1]) ? $hostname_matches[1] : null; // This is the capture of the whole config. // After executing the "show run" command, we are going // to tell our socket to keep reading until they find either: // 1. "--More--", // 2. "end" (which would correspond to the end of the config) or // 3. the hostname of the current device // The regex pattern tries to match any of these possibilities // while being careful not to hit the wrong "end" string // (hence the special chars before and after seen in the pattern). // Also, we are going to send a SPACE char all the way until the end. $output = ''; if (!is_array($commands)) { $commands = [$commands]; } $escaped_hostname = str_replace("/", "\\/", $hostname); $escaped_hostname = str_replace("-", "\\-", $escaped_hostname); $escaped_manager_hostname = str_replace("/", "\\/", $manager_hostname); $escaped_manager_hostname = str_replace("-", "\\-", $escaped_manager_hostname); $endingDelimeters = ["{$escaped_hostname}([\\w()\\-]*)(#|>)", "{$escaped_manager_hostname}([\\w()\\-]*)(#|>)"]; foreach ($commands as $command) { $ssh->write($command . "\n"); while ($section = $ssh->read("/--More--|" . implode("|", $endingDelimeters) . "/", NET_SSH2_READ_REGEX)) { // This condition is met when the current user has no access // to the "show run" command or the command is not available in the device. if (strpos($output, "Invalid input detected") !== false) { static::log("{$node->ip_address} comando `{$command}` no disponible." . PHP_EOL); } $output .= $section; $ssh->write(" "); if (preg_match("/" . implode("|", $endingDelimeters) . "/", $section)) { break; } } // $ssh->read("/(#|>)/", NET_SSH2_READ_REGEX); } // Exiting out of this NE, to continue with another one // in our next iteration of this loop $ssh->write(" exit\r\n"); // Let's clean the output containing all of the configuration by: // 1. removing the "show run" command string // 2. removing all "--More--" strings up to the next valid character. // 3. removing special characters and "[K", which for some reason is present in some outputs $output = trim(str_replace(["show run\r\n"], "", $output)); $output = preg_replace("/ --More--.+?(?=([*!\nA-z0-9]))/", "", $output); $output = str_replace([chr(27), "[K"], "", $output); return $output; }
/** * Creates a default exception with default exception message * containing type/method & location of the code that * invokes an public static accessor of this class. * * @param string $namespace_ * * @return \Components\Deprecated */ private static function createDefaultException($namespace_) { $exception = new static($namespace_, null, null, false); $stackTrace = $exception->getTrace(); $exception->message = sprintf('Deprecated use of [type: %1$s, method: %2$s, location: %3$s:%4$s].', $stackTrace[2]['class'], $stackTrace[2]['function'], isset($stackTrace[2]['file']) ? $stackTrace[2]['file'] : '{internal}', isset($stackTrace[2]['line']) ? $stackTrace[2]['line'] : 0); $exception->log(); return $exception; }
public function __construct($debug = false) { static::$log = new \Fit\Log(); $this->profile = new \Fit\ProductProfile(); $this->debug = (bool) $debug; }
/** * Set a logger * @param Log $log */ public static function log($log) { static::$log = $log; }