logger && function_exists( 'wc_get_logger' ) ) { $this->logger = wc_get_logger(); } return $this->logger; } /** * @param string $message Message. * @param array $context Extra context (phone is masked automatically). */ public function info( $message, $context = array() ) { $this->log( 'info', $message, $context ); } /** * @param string $message Message. * @param array $context Extra context. */ public function warning( $message, $context = array() ) { $this->log( 'warning', $message, $context ); } /** * @param string $message Message. * @param array $context Extra context. */ public function error( $message, $context = array() ) { $this->log( 'error', $message, $context ); } /** * @param string $message Message. * @param array $context Extra context. */ public function debug( $message, $context = array() ) { $this->log( 'debug', $message, $context ); } /** * Write a log line. * * @param string $level Log level. * @param string $message Message. * @param array $context Extra context. */ private function log( $level, $message, $context ) { $logger = $this->logger(); if ( ! $logger ) { return; } $extra = $this->stringify( $this->mask( (array) $context ) ); if ( '' !== $extra ) { $message .= ' ' . $extra; } $logger->log( $level, $message, array( 'source' => 'sendly' ) ); } /** * Mask sensitive context values. * * @param array $context Context map. * @return array */ private function mask( $context ) { if ( isset( $context['phone'] ) ) { $context['phone'] = PhoneNormalizer::mask( (string) $context['phone'] ); } // Never log secrets even if accidentally passed. unset( $context['token'], $context['secret'], $context['Authorization'] ); return $context; } /** * Turn a context map into a compact "k=v" string. * * @param array $context Context map. * @return string */ private function stringify( $context ) { $parts = array(); foreach ( $context as $k => $v ) { if ( is_scalar( $v ) || null === $v ) { $parts[] = $k . '=' . $v; } else { $parts[] = $k . '=' . wp_json_encode( $v ); } } return $parts ? '[' . implode( ' ', $parts ) . ']' : ''; } }